I am a beginner in Java and I'm currently facing a problem that I'm not sure how to qualify. I'm trying to do a login in my app. Here's part of my code:
PreparedStatement statement = connect.prepareStatement("SELECT `userName`,`password` FROM `member` ");
//creating a variable to execute query
ResultSet result = statement.executeQuery();
//while (result.next()) {
if (result.next()) {
if (result.getString("userName") != getUserNameTextField()) {
System.out.print("DB " + result.getString("userName") + " TF " + getUserNameTextField() + "\n");
System.out.print("Incorrect username");
//break;
} else if ......
}
There are no compiler errors, the project is built and run successfully in Eclipse. I was initially only testing if both username and password match, then successful login; else unsuccessful login. Since I was receiving the unsuccessful login output, I then tried to determine if it was the username or password that was incorrect. The output pointed out that it was the username, and since I couldn't figure out what I was doing wrong, I decided to print both the username retrieved from my DB and the one I entered in the textfield. The result of the print statement in the console in Eclipse is as follows:
DB Smith TF Smith
Incorrect username
The username retrieved from my DB is Smith and the one entered in the textfield is also Smith, and there are no errors in Eclipse, so now I'm even more confused.
I was using while (result.next()){...login validation code..} first; after some research, I also tried if (result.next()) { do {..login validation code..} while (result.next()} - but everything I tried results in the same output in Eclipse.
I have a vague impression that I should be using the while (result.next()) itself and that this problem may have to do with the Java memory model (heap and stack), but I'm not sure. Can anyone point me in the right direction? Thanks.