File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Unable to access second row in resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Unable to access second row in resultset" Watch "Unable to access second row in resultset" New topic
Author

Unable to access second row in resultset

Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
Hi,
I am trying to work on user authentication.
There are 2 rows in a mysql table. I am trying to validate them by comparing them with the inputs from 2 input fields (username and password) on my jsp page.

When I login using data from the first row of the table, I am forwarded to success.
But when I login using the data from the second row, I am forwarded to the error page. I believe the resultset is not moving forward to the next row.

I have pasted the code below.
Where am I going wrong?

Many thanks in advance.



-- Aparna
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61428
    
  67

Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

For more information, please read this.

This post has been moved to a more appropriate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

That isn't really surprising, if you look at the code.

You read the first row and extract the two columns from it. Then you have an if-else structure in which all three branches end with "return". There's no way for the code to ever carry on and read the next row.

But what you are doing wrong is using a query which can return more than one row. A PreparedStatement containing a query like "select 1 from table where user = ? and password = ?" would be much better. Then if one row was returned you have a correct password input, and if zero rows were returned then you don't.
Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
Thanks for the reply.

So if I was to use a prepared statement, then I don't really need to remove the 'return' statement, since there is only going to be one row in the resultset. Do I?
Please correct me if I am wrong.

Thanks!
Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
Hi,
I changed to Prepared statement. And now I am able to login successfully for the 2 users in my table. But when I enter gibberish, I am not forwarded to the error page. Nothing happens! No exceptions either.

Any help here please?

Thanks again, I am new around to coding.

Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

the point is you want to continue to extract the usernames and password when both of them are not null.
So as Paul Clapham was talking about, if you put up a return in that case it is going to generate just one result and its going to return right?

-kolli
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Aparna Ram wrote:
when I enter gibberish, I am not forwarded to the error page. Nothing happens! No exceptions either.



You're query is only selecting things that are equal to your name and pass. If you select only those things when you enter gibberish you are going to get nothing back and skip your whole rs.next logic including the page setting.

I assume your userName is a unique field, I don't know how you would do a login based on usrname and pass if it wasn't. So you can just run your query and if it returns anything authorize them.


It's also redundant to have a select based on usr name and password and then check what's returned vs the input used in the select.
Aparna Ram
Ranch Hand

Joined: Jan 27, 2006
Posts: 59
Thanks Paul Yule!
That worked beautifully :-)
-- Aparna
 
jQuery in Action, 2nd edition
 
subject: Unable to access second row in resultset