Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check if the resultset is empty

 
Nawed Shaikh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
i got stuck in this code :

ResultSet result= stmt.executeQuery("Select password from checker where username = '"+user+"'");
if(rs==null){
System.out.println("Wrong Username");
}
if the input username of the user will be wrong then the reference result will be empty.
this code compiles but if user variable is wrong(that is not in database) it shows database exception
So how do i make sure this exception doesnt occur.
Sorry for writing all these in quotes and i know this is not right method but i am new and dont know how to post it correctly
Thank you in advance

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the javadocs of the ResultSet interface - it has a method which tells you exactly that (whether or not the ResultSet has any data). Since ResultSet has lots of methods, I'll give you a hint: the method returns a boolean.

I'm not sure why you'd think that the ResultSet returned might be null - the javadocs of Statement.executeQuery specifically say that it will never be null.
 
Nawed Shaikh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh you mean

rs.next() will return boolean.
Well thank you sir it worked!


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic