根据不同的ID开头从数据库更改密码

时间:2019-04-04 07:36:15

标签: java mysql database drag-and-drop jcomponent

我有3个表(医生,护士,患者),它们的ID开头都不同,医生的ID开头为101,护士的ID开头为102,患者的开头为200。 我想根据其ID的开头更改密码。 在我的JFrame中,我有5个JComponents,4个Jtextfields,1个Jbutton 1个Jtextfields作为id(名称:idField) 当前密码的1个Jtextfields(名称:currentPass) 2个用于新密码的Jtextfields(名称:newPass1,newPass2) 1个用于操作的Jbutton(名称:changeButton)

我在代码中做了2种不同的方式,但两种方式都不适合我。 您能帮我解决这个问题吗?

第一种方式:

private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
       id=idField.getText();
       newpass1=newPass1.getText();
       newpass2=newPass2.getText();

        try {
        con = DriverManager.getConnection("jdbc:derby://localhost:1527/hj", "xxx", "xxx");
        st = con.createStatement();

        if (newpass1.equals(newpass2)){


          ResultSet rs = st.executeQuery("update patient set patient_Password="+ newpass1 +" where patient_Id="+id+" and patient_Id like '200%'");  
          JOptionPane.showMessageDialog(this , "Successfully changed", "Patient password successfuly changed !",JOptionPane.PLAIN_MESSAGE);

          ResultSet rs1 = st.executeQuery("update Nurse set nurse_password="+ newpass1 +" where nurse_id="+id+" and nurse_id like '102%'");
            JOptionPane.showMessageDialog(this , "Successfully changed", "Nurse password successfuly changed !",JOptionPane.PLAIN_MESSAGE);

            ResultSet rs2 = st.executeQuery("update doctor set doctor_password="+ newpass1 +" where doctor_id="+id+" and doctor_id like '101%'");
            JOptionPane.showMessageDialog(this , "Successfully changed", "Doctor password successfuly changed !",JOptionPane.PLAIN_MESSAGE);

        } else 
            JOptionPane.showMessageDialog(this , "Not equal", "Your new passwords are not equal!! , try again",JOptionPane.ERROR_MESSAGE );
       }catch (Exception x){
           JOptionPane.showMessageDialog(this, x.getStackTrace());
       }
    }

第二种方式:

 private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
       id=idField.getText();
       newpass1=newPass1.getText();
       newpass2=newPass2.getText();

        try {
        con = DriverManager.getConnection("jdbc:derby://localhost:1527/hj", "xxx", "xxx");
        st = con.createStatement();

        if (newpass1.equals(newpass2)){

        if (id.startsWith("200")){
          ResultSet rs = st.executeQuery("update patient set patient_Password="+ newpass1 +" where patient_Id="+id+"");  
          JOptionPane.showMessageDialog(this , "Successfully changed", "Patient password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
        } 
        else if (id.startsWith("102")){
          ResultSet rs = st.executeQuery("update Nurse set nurse_password="+ newpass1 +" where nurse_id="+id+"");
            JOptionPane.showMessageDialog(this , "Successfully changed", "Nurse password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
                }
        else if (id.startsWith("101")){
            ResultSet rs = st.executeQuery("update doctor set doctor_password="+ newpass1 +" where doctor_id="+id+"");
            JOptionPane.showMessageDialog(this , "Successfully changed", "Doctor password successfuly changed !",JOptionPane.PLAIN_MESSAGE);
        }

        } else 
            JOptionPane.showMessageDialog(this , "Not equal", "Your new passwords are not equal!! , try again",JOptionPane.ERROR_MESSAGE );
       }catch (Exception x){
           JOptionPane.showMessageDialog(this, x.getStackTrace());
       }
    }

1 个答案:

答案 0 :(得分:2)

请使用$responstable

PreparedStatement

通过串联查询,您将得到 if (id.startsWith("200")){ try (PreparedStatement pstmt = conn.prepareStatement("UPDATE patient SET patient_passwort=? WHERE patient_id=?");) { pstmt.setString(1, newpass1); pstmt.setString(2, id); int rows = pstmt.executeUpdate(); JOptionPane.showMessageDialog(this , "Successfully changed", "Patient password successfuly changed! (updated rows: "+rows+")", JOptionPane.PLAIN_MESSAGE); } } 。 未引用新密码(此处为update patient set patient_Password=abcdefghi where patient_Id=200340 and patient_Id like '200%'),这对于查询中的字符串是必需的。 abcdefghi也未加引号,但可能是数字字段,不必加引号。

顺便说一句:

  • 不需要查询部分patient_id

  • 您应该关闭任何PreparedStatement / Statement实例,这可以通过使用try-with-resources(patient_id like '200%')来完成。 try (PreparedStatement xxx = ...) { ... your code } // closes automaticallyConnection也是如此。

  • 由于ResultSet是整数,因此您可能需要这样使用它:id

提示: 如果使用Apache commons-dbutils,您将更加轻松自在。例如int updId = Integer.parseInt(id); ... pstmt.setInt(2, updId); ...

org.apache.commons.dbutils.QueryRunner