Solution to the problem of JPasswordField's getText() method being out of date

Posted by drunkencelt on Sat, 07 Dec 2019 06:02:41 +0100

In recent days, I want to make a login interface with Jframe. When connecting to the database, I found that the getText() of JPasswordField is out of date and cannot be used. After checking the data, we found that it was changed to:

 try{
                String sql="SELECT * FROM username WHERE name=?";

                conn = DB.getConnection();
                prst = conn.prepareStatement(sql);
                String lll= Login.getText();
                System.out.println(lll);
                prst.setString(1,lll);

                prst.execute();
                rs = prst.executeQuery();
                if (rs.next()){

                    if( String.valueOf( regi.getPassword()).equals(rs.getString(3))){
                   
                        JOptionPane.showMessageDialog(Main, "Success", "error",JOptionPane.WARNING_MESSAGE);
                    }else {
                        System.out.println(rs.getString(3));
                        System.out.println(regi.getPassword().toString());
                        JOptionPane.showMessageDialog(Main, "Account password error", "error",JOptionPane.WARNING_MESSAGE);
                    }
                }else{
                    JOptionPane.showMessageDialog(Main, "No such user", "error",JOptionPane.WARNING_MESSAGE);
                }
            }catch (Exception e1){
                e1.printStackTrace();
            }     finally {
                DB.close(rs);
                DB.close(prst);
                DB.close(conn);
            }
        }

 

  • if(regi.getPassword.toString.equals(rs.getString(3)))){

     

...... / / the columns in the database start from 1, so the password is three columns

}

The following errors occur:

The second is System.out.println(regi.getPassword.toString);

Check the data and find that the toString method is the object method, and the returned method is HashCode

 

Change to

  • if( String.valueOf( regi.getPassword()).equals(rs.getString(3))){

     

.................

}

It is completely correct. The string of the password is successfully extracted.

Also, it is not allowed to use = =, so the comparison is whether the memory location is the same.

Use. equals() to compare whether the strings are the same.

Topics: Java Database SQL