检查数据库中的当前密码(现有值)

时间:2019-04-07 16:06:18

标签: java mysql prepared-statement

我有一个框架来编辑用户(患者)的密码,并且我做了很多方法来检查用户输入的密码是否与数据库中的当前密码匹配,我尝试了(.isBeforeFirst())和(.next()),结果集和prepared语句,并且我每次都会更改布尔值,以查看代码中是否有错误,但有时即使密码错误也有时会更改密码,有时它根本不会更改密码即使是正确的。请告诉我出了什么问题,然后告诉我正确和简便的检查方法:(

这是我当前的代码:

Connection con;
        Statement st;
        PreparedStatement ps;
        ResultSet rs;

    public boolean checkPassPatient(String id,String pass) {
        boolean flag=false;
        try{
            con = DriverManager.getConnection("jdbc:derby://localhost:1527/hj", "xxx", "xxx");
//            st=con.createStatement();
//            rs=st.executeQuery("select patient_password from patient where patient_id='"+id+"'");
            try (PreparedStatement pstmt = con.prepareStatement("select patient_password from patient where patient_id=?");) {
          pstmt.setString(1, id);
          int rows = pstmt.executeUpdate();
          if (pstmt.getResultSet().equals(pass))
                flag= true;
            else
                flag= false;
            }

        }catch(SQLException ex){

        }
        return flag;
    }

在这里,我如何调用该方法:

if (checkPassPatient(id,current)==true){
          try (PreparedStatement pstmt = con.prepareStatement("UPDATE patient SET patient_password=? WHERE patient_id=?");) {
          pstmt.setString(1, newpass1);
          pstmt.setString(2, id);
          int rows = pstmt.executeUpdate();
          JOptionPane.showMessageDialog(null , "Patient password successfuly changed!", 
             "Patient password successfuly changed! (updated rows: "+rows+")", JOptionPane.PLAIN_MESSAGE);
       }
             } else 
                 JOptionPane.showMessageDialog(null , "Your current passwords not true !! , try again", "Your new passwords are not equal!! , try again",JOptionPane.ERROR_MESSAGE );

0 个答案:

没有答案