1

I'm building this component where I can change the password that is stored in my MySQL database using Java Eclipse. I have this typical system where you have to enter your old password and new password and have that old password checked to match the one in the system.

public class ChangePassword implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        JPasswordField oldPswrd = new JPasswordField(50);
        JPasswordField newPswrd = new JPasswordField(50);
        JLabel j = new JLabel("Type in old password: ");
        JLabel j2 = new JLabel("Type in new password: ");
        j.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        j2.setFont(new Font("Times New Roman", Font.PLAIN, 15));
        
        Object[] message = {
                j, oldPswrd,
                j2, newPswrd,
            };
        
        int result = JOptionPane.showConfirmDialog(null, message, 
                 "CHANGE PASSWORD", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
           char[] oldp = oldPswrd.getPassword();
           String oldP = String.valueOf(oldp);
           char[] p = newPswrd.getPassword();
           String newP = String.valueOf(p);
           System.out.println("New name: " + newP);
//// PROBLEM SEEMS TO START HERE ///////////////////////////////////////////
           try {    
               Connection connection = DriverManager.getConnection(url, username, password);
               System.out.println("connection success!!");
               
               String sql = "SELECT * FROM user_info WHERE user_id = ? && user_password = ?";
               PreparedStatement statement = connection.prepareStatement(sql);
               statement.setString(1, user_id);
               statement.setString(2, password);
               
               ResultSet rs = statement.executeQuery();
               if (rs.next()){
                   String oldPassword = rs.getString("user_password");
                   if (oldP.equals(oldPassword)) {
                       String sql1 = "UPDATE user_info SET user_password = ? WHERE user_id = ?";
                       PreparedStatement statement1 = connection.prepareStatement(sql1);
                       statement1.setString(1, newP);
                       statement1.setString(2, user_id);
                       
                        int rows2 = statement1.executeUpdate();
                        if (rows2 > 0) {
                            System.out.println("UPDATE PASSWORD!");
                        }
                   }
               } else {
                   JLabel l = new JLabel("Old password does not match");
                   l.setFont(new Font("Times New Roman", Font.PLAIN, 15));
                   l.setHorizontalAlignment(SwingConstants.CENTER);
                   JOptionPane.showMessageDialog(null, l, "ERROR", JOptionPane.PLAIN_MESSAGE);
               }
            } catch (SQLException e) {
                System.out.println("& i oop");
                e.printStackTrace();
            }
///////// PROBLEM SEEMS TO END HERE   ///////////////////////////////////////////
           frame.setVisible(false);
           new UserMain(user_id, name, newP);
    }
        
    }
}

I have this table with a row of data already set in as a test. However, whenever I try to change the password - typing in the correct old password and typing in another new one - it always shows

old password does not match

There's no syntax or connection errors so I figured the problem is a logic error or something I couldn't catch. Also, the java portions work fine, I've checked those already. The problem seems to be between the try and catch parts of my code.

2 Answers 2

2

password is your database password, not one of the user passwords. Change

statement.setString(2, password);

to

statement.setString(2, oldP);
Sign up to request clarification or add additional context in comments.

1 Comment

You found the problem! Thank you so much!!
0

Where are you taking this "username" and "password" from?

Also you are passing the same "password" variable to find the user that is using for the DB connection. I think it should be change to "oldP ".

1 Comment

The username and password are both passed from my other function that this function stems off from. And yes, that seems to be the problem. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.