java—如何使用名称将id值从一个表传递到另一个表,并将信息存储到数据库

s4n0splo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(417)

这个问题在这里已经有答案了

java.sql.sqlexception参数索引超出范围(1>参数个数,为0)[已关闭](2个答案)
两年前关门了。
sql查询如何使用department表中的department id将id从使用department name的department表传递到使用department表中的department id的user表depart\u id是主键,而user表中的depart\u id是外键
如何使用department\u name从department表中选择depart\u id并将值存储在user表中

try{
                Connection con = DBconnect.getConnection();

                //selecting the dpartment
                String sql ="select DEPARTMENT_CODE,DEPARTMENT_NAME from department_info";
                PreparedStatement ps = con.prepareStatement(sql);
                String s11=comboboxdeptid.getItems().toString();
                ResultSet rs=ps.executeQuery();

                if(rs.next()==true)
                {
                    if(rs.getString("DEPARTMENT_NAME").equals(comboboxdeptid.getSelectionModel().toString()))
                        rs.getString("DEPARTMENT_CODE");
                }

                //second stmt
                String sql1 = "insert into user_info(USER_NAME, FIRST_NAME, LAST_NAME, DESIGNATION, ADDRESS,PASSWORD_TXT,DEPARTMENT_CODE,CREATED_BY) values(?,?,?,?,?,?,?,?)";
                PreparedStatement ps1 = con.prepareStatement(sql1);
                    String s12 = nameid.getText();
                    String s13 = Firstnameid.getText();
                    String s14 = Lnameid.getText();
                    String s15 = desigid.getText();
                    String s16 = comboboxdeptid.getItems().toString();
                    String s17 = addrsid.getText();
                    String s18 = passwordid.getText();
                    ps.setString(1, s12);
                    ps.setString(2, s13);
                    ps.setString(3, s14);
                    ps.setString(4, s15);
                    ps.setString(5, s17);
                    ps.setString(6, s18);
                    ps.setString(7, s11);
                    ps.setString(8, "abc");
                    ps.execute();
                    ResultSet rs1=ps1.executeQuery();

                    //third stmt
                    String sql2 = "update security_qa_info set SECURITY_QUESTION=?, SECURITY_ANSWER=? where USER_ID=?";
                    PreparedStatement ps2 = con.prepareStatement(sql2);
                    String s19 = securityquestionid.getSelectionModel().getSelectedItem().toString();
                    String s20 = answerid.getText();
                    while(rs2.next()==true)
                    {
                        if(rs2.getString("USER_NAME").equals(nameid.getText()))
                        {
                            rs2.getString("USER_ID");
                            ps2.setString(1, s16);
                        }
                    }
                    ps2.setString(2, s19);
                    ps2.setString(3, s20);
                    ps2.executeUpdate();

                    showMessageDialog(null, "Registration Successful");
            }catch(Exception e){
        //         showMessageDialog(null, e);
          e.printStackTrace();
            }
            Parent fxml = FXMLLoader.load(getClass().getResource("/com/abc/fxml/LoginPage.fxml"));
            pane2.getChildren().setAll(fxml);

        } else {
        showMessageDialog(null, "Passwords don't match!");
    }

}
4zcjmb1e

4zcjmb1e1#

ps=为select查询准备的语句:

String sql ="select DEPARTMENT_CODE,DEPARTMENT_NAME from department_info";
      PreparedStatement ps = con.prepareStatement(sql);

ps1=为insert语句准备的语句:

String sql1 = "insert into user_info(USER_NAME, FIRST_NAME, LAST_NAME, DESIGNATION, ADDRESS,PASSWORD_TXT,DEPARTMENT_CODE,CREATED_BY) values(?,?,?,?,?,?,?,?)";
      PreparedStatement ps1 = con.prepareStatement(sql1);

使用错误的准备语句:

ps.setString(1, s12);

一个建议——如果你把第一个准备好的语句叫做“selectdepartmentdetails”,第二个叫做“insertuserinfo”,那么你就不太可能遇到这种情况。

相关问题