Hi all!
i have a question.
i have a standalone java swing program that i use with postgreSQL jdbc. it has a login page which can compare the textfield with the value in database.
this is part of my code:
problem is, when i entered the correct username and password, the "incorrect login and password" warning JOption still pops out!
can you tell me how to fix this? thank you in advance!
Are you storing the password unencrypted in the database?
Generally speaking, selecting * from a table is a bad idea. Similarly getting a value from the resultset based on the index is a bad idea. There is the potential here that you are getting the wrong values. I strongly recommend you change that to (changing field names as appropriate):
Now if you were to enter "user-b/password-b" - what would happen on the very first loop through your code?
Alternatively, if you had entered "user-a/password-a", what would have happened on the second loop through your code? (Why do you allow a second loop through your code?)
Which goes back to what I was saying earlier - is your code that displays the "Incorrect login or password" in the right place? Is there a better way of determining whether any of the usernames/passwords matched before showing that?
i think i put the if else code in the wrong place..because it iterates my record (i think)..
this is because when i log in..the incorrect password warning flashes 2 times when i entered user-a/password-a
can you suggest where i was to put the if else code?
Try this using prepared statement
PreparedStatement pstmt;
String sql="Select * from login where username=? and password=?"
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,password);
ResultSet rs=pstmt.executeQuery();
The above code it self will satisfy your purpose, then what is the below code. Why are you checking once again?
just by doing this your purpose should get solved, with an assumption that username and password forms a unique record
im doing that because i wanna compare if the user inputs the right set of username and password.
if i follow your suggestion, then how to compare the password part?
I think Swastik is just misreading your SQL - assumign it check by both username and password. If you have already matched your user by username and password in SQL, what extra functionality does your check in Java provide? You only check by username; if you change your SQL to check by both credentials your Java code is redundant.