1

I am working on a project for school which requires a very basic log in function (nothing too fancy, seeing as how the passwords will remain in plain text). I have a test database and have thoroughly tested that all values that are being referenced and pulled from the database can also be displayed properly. You will see in the code that the type for each variable being compared is of the String type. My question to you is, why aren't the variables userOut and un equal? (I have even tried using the .toString() method on each of them individually, and well as together to try and get them "equal").

    public static void main(String[] args) throws SQLException {

    try {

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "Hsiri0758");
        System.out.println("Database connected...");

        Statement stat = conn.createStatement();
        Statement pwstat = conn.createStatement();

        Scanner scan = new Scanner(System.in);
        System.out.println("Username: ");
        String un = scan.nextLine();
        System.out.println("Password: ");
        String pw = scan.nextLine();

        ResultSet unRset = stat.executeQuery("SELECT username FROM accounts WHERE username = \"" + un + "\";");
        unRset.next();
        String userOut = unRset.getString(1);

        ResultSet pwRset = pwstat.executeQuery("SELECT password FROM accounts WHERE password = \"" + pw + "\";");
        pwRset.next();
        String pwordOut = pwRset.getString(1);

        if (userOut == un) {
            System.out.println("Welcome " + un + "!");
        }
        else {
            System.out.println("Invalid username.");
        }

        conn.close();
        }
        catch (SQLException ex) {
        ex.printStackTrace();
        }

    }
draztick
  • 31
  • 3

7 Answers7

2

replace this line if (userOut == un) { to if (userOut.equals( un)) {

Please see this for more info on == and .equals()

Community
  • 1
  • 1
2

Apart from primitives like 1 you have to compare values with the equals() method in java.

So in your code this means: userOut.equals(un).

In the end you'll end up with:

if (userOut.equals(un)) {
    System.out.println("Welcome " + un + "!");
}

If you have the opportunity you should compare a known String to the input because you can avoid NullPointerExceptions.

A common problem:

public void someMethod(String someParameter) {
    if(someParameter.equals(MY_CONSTANT)) {
        // ...
    }
}

This should be refactored to:

public void someMethod(String someParameter) {
    if(MY_CONSTANT.equals(someParameter)) {
        // ...
    }
}
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
0

If you are comparing Strings try to use equals instead of ==

You can also use equalsIgnoreCase to avoid case sensitive problems

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
0

To compare objects in java use .equals() method instead of "==" operator

Replace the following code

 if (userOut == un) 

to

 if (userOut.equals(un)) 

if you want to ignore the case use like below

 if (userOut.equalsIgnoreCase(un)) 
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

When you use the == operator in Java, you are simply seeing if the reference variables reference the same string. Therefore, in your case, this will always evaluate as false.

To simply compare strings try userOut.equals(un). This checks the actual contents of the strings to see if they are equal. If they are, it will return true.

For more info check out:
http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/

Bryan Muscedere
  • 572
  • 2
  • 10
  • 23
0

== is used in client side scripting like javascript or jquery. Server side would need equals or equalsIgnoreCase.

SRay
  • 286
  • 1
  • 5
0

This is because String is a class, not a datatype. You are acutally only comparing two class references with each other and not the actual strings. You must use equal() provided by the class-designer to compare the contents of the two instances of String:

userOut.equals(un)
whetstone
  • 635
  • 6
  • 11