0

I'm working on a student database managing program, and am working on the login phase. I query the database for the students id and password and try and match an entry to what the student enters. When a match is found login should be set to true, and it should go to the next page, but for some reason its never passing my if then statement. I'm not entire sure whats wrong,the code for this is below. You'll notice the system.out.println statements, those are there so I can see if it is actually going through the database correctly, and as far as I can tell it does, but login next gets set to true. I would really appreciate any help.

public void actionPerformed(ActionEvent e)
    {
        //int i;
        //String name;

        if(e.getSource()==logInButton)
        {
            String name="";
            String password="";

            name=inputField.getText();
            password=inputField2.getText();

            System.out.println(name);
            System.out.println(password);

            boolean login = false;

            try {
                connection = DriverManager.getConnection(connectionString, username, pass);
                PreparedStatement statement = (PreparedStatement) connection.prepareStatement("SELECT * FROM students");
                data = statement.executeQuery();
                while(data.next()){
                    System.out.println(data.getObject("student_id"));
                    System.out.println(data.getObject("password"));
                    if (data.getObject("student_id") == name && data.getObject("password") == password){
                        login = true;

                    }
                }
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(login == true){
                System.out.println("login = true");
                logInPanel.setVisible(false);
                postLogInPanel.setVisible(true);
            }
        }

1 Answers1

0

In Java, == is object identity. a==b is true if a and b are references to the same object in memory. When you're comparing reference types, like Strings and possibly whatever e.getSource() returns, you should use the equals method to see if the values are the same.

Andbdrew
  • 11,788
  • 4
  • 33
  • 37
  • Unless, of course, those columns are nullable in the database; in which case it would be safer to reverse those conditions, like `name.equals(data.getObject("student_id"))`. And did you really mean to compare the contents of the `name` field with the `student_id` value from the database? – Dawood ibn Kareem Dec 01 '14 at 03:00
  • Yes, originally I had the field in the database named name and I just haven't changed it yet, thanks for the help. – user3082473 Dec 01 '14 at 05:39
  • I've changed it to if (name.equals(data.getObject("student_id")) && password.equals(data.getObject("password"))) but for some reason its still not working??? – user3082473 Dec 01 '14 at 05:41