Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Log On help

 
steve dowdall
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Partners,
I have a simple log on form that a user types in their username and password into to access my application. The question I have is how do I loop through the DB table to check if the username and password exist in the DB and are correct? If they are I will return a flag value of 1 to launch the main app form, if they do not exist then I will return a flag value of -1 and launch an error dialog box. I have all the pieces working except for the sql and java to check the DB.
Please help....
Cheers,
Steve
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you just need to write a normal SQL statement to retrieve any hits agains that username
PreparedStatement sql = "SELECT username passwd FROM userTable WHERE username = inputFromDialog";
Then check the ResultSet, and if it is empty there were no hits, if it has data, check to see if the password retrieved matches the passwork on the Dialog.
 
steve dowdall
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy,
You have helped me a lot in the past. Good to hear from you again.
I afraid that's the part I'm stuck on. What does the code look like to loop through the recordset?
Steve
 
steve dowdall
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try this:
try{


System.out.println("Recordset before = " + " " + result);
Statement stmt = con.createStatement();
String qs = new String ("SELECT logonname,logonpassword FROM users WHERE logonname = " + " " + user + " " + " " + "AND logonpassword = " + " " + password );
String qs = new String ("SELECT * FROM users" );
result = stmt.executeQuery(qs);
System.out.println("Recordset after = " + " " + result);
}

I get this:
Looking for Database Connection....
Jdbc driver loaded succesfully.
DB Connection found..
Recordset before = null
Recordset after = sun.jdbc.odbc.JdbcOdbcResultSet@581784
BTW, it also seems to loop through this twice however, I have no loop set up yet.
Looking for help....
Steve
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you got data, so life is good.
You can't just print a ResultSet as though it were a String. It isn't. It also could have many rows in it, and println is not capable of handling that. Therefore it is just printing the source of the data.
You could use result.getString(0) to look at the first row and print it, and you could loop through the rows to print them one row at a time.
To read through your result use something like:
boolean foundID = false;
while(result.next()){
String gotName = result.getString(logonname);
String gotPW = result.getString(logonpassword);
foundID = true;
// do whatever
}
When the set hits end of file the while loop will break.
 
steve dowdall
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Cindy. Life is good.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic