Forums Register Login

Invalid cursor state exception problem from resultset

+Pie Number of slices to send: Send
Hi ,
For the below mentioned code I am getting the invalid cursor state when the query does not return any result from the database.Please let me know how to tackle this situation so that invalid cursor state can be handled



private String queryLogin;
private String dbUsername;
private String dbPassword;

Connection connection=new ForumConnection().getConnection();
ResultSet resultset=null;
/*
** Writting and executing query for getting usernames and respective password from Login table
*/
queryLogin="select * from Login where username='"+username+"';";

try {
Statement statement=connection.createStatement();
resultset=statement.executeQuery(queryLogin);

resultset.next();
if(resultset!=null){
/* Here the Invalid cursor state exception is thrown */
dbUsername=resultset.getString("Username");
dbPassword=resultset.getString("Password");
}

} catch (SQLException e) {
System.out.println(" Query problem for Login--"+e);
}

if(dbUsername.equals(username) && dbPassword.equals(password) ){
return true;
}
else{
return false;
}

Basically the above code takes the username and password as input string and checks itin the database.If it doesn't exists then it return false.

Regards,
+Pie Number of slices to send: Send
If the resultset is empty, rs.next will return false and you cannot read any data from it - there is nothing to read and you get an illegal cursor position because you're not 'looking' at anything.

ie you should have something like this:


The username and pasword won't be set, but that will be because the query failed.

Dave
+Pie Number of slices to send: Send
Your code

resultset.next();
if(resultset!=null){
/* Here the Invalid cursor state exception is thrown */
dbUsername=resultset.getString("Username");
dbPassword=resultset.getString("Password");
}

You have done the checking as resultset!=null. Instead modify your code like this

if(resultset.next()){
dbUsername=resultset.getString("Username");
dbPassword=resultset.getString("Password");
}

Look at explanation for ResultSet's next method in API.
+Pie Number of slices to send: Send
Thanks for the reply and I am sorry I realised later on it by method's signature only that this method returns Boolean and i was handling null and notnull :-).

Anyways, Thanks again for the response.
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5477 times.
Similar Threads
Edit or update
is there a any validation API in java
JSP validation
problem with jsp page and java bean
DB connection and a null pointer exception
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:58:15.