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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据不同的id开头从数据库更改密码相关的知识,希望对你有一定的参考价值。

我有3张桌子(医生,护士,病人),他们都有不同的身份证明开始,医生身份从101开始,护士从102开始,患者从200开始。我想根据他们的id的开头更改密码。在我的JFrame中我有5个JComponents,4个Jtextfields,1个Jbutton 1 Jtextfields用于id(name:idField)1个Jtextfields用于当前密码(name:currentPass)2个Jtextfields用于新密码(name:newPass1,newPass2)1个Jbutton for动作(名称:changeButton)

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

第一种方式:

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());
       
    
答案

请使用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也没有引用,但它可能是一个数字字段,不得引用。

BTW:

  • 不需要查询部分patient_id like '200%'
  • 您应该关闭所有PreparedStatement / Statement实例,这可以通过使用try-with-resources(try (PreparedStatement xxx = ...) ... your code // closes automatically)来完成。同样适用于ConnectionResultSet
  • 因为id是一个整数,你可能想要这样使用它:int updId = Integer.parseInt(id); ... pstmt.setInt(2, updId); ...

提普:如果你使用Apache commons-dbutils,你会更加轻松。例如org.apache.commons.dbutils.QueryRunner

  QueryRunner r = new QueryRunner();
  int rows = r.update(conn, 
       "UPDATE patient SET patient_passwort=? WHERE patient_id=?",
       newpass1, id);

以上是关于根据不同的id开头从数据库更改密码的主要内容,如果未能解决你的问题,请参考以下文章

怎么更改电脑的ID和密码?

怎么更改电脑的ID和密码

在没有DBA权限的情况下更改不同数据库下多个模式的密码?

MYSQL的ROOT更改密码之后不能登陆?

如何从 Datagrip 更改 Oracle 密码?

在忘记APPLE ID密码的情况下如何删除icloud账号?