File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Log On help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Log On help" Watch "Log On help" New topic
Author

Log On help

steve dowdall
Ranch Hand

Joined: May 23, 2001
Posts: 36
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
steve dowdall
Ranch Hand

Joined: May 23, 2001
Posts: 36
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

Joined: May 23, 2001
Posts: 36
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: May 23, 2001
Posts: 36
Thank you very much Cindy. Life is good.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log On help
 
Similar Threads
Always responding invalidLogin.jsp
My project design
Displaying the form values.
Understanding Sessions
View cannot be restored exception