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

Quering the ResultSet

 
kunal Jaggi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat 4.1.12 on Windows 2000 professional. I am using the JDBC-ODBC Bridge driver to access the MS Access Database. I am having a problem while quering the ResultSet. Following is the code snippet that gives the problem :-
try{
rs=st.executeQuery("SELECT * FROM Users WHERE cID='"+f.getUserName()+"'");
if(rs==null)
userExists=false;
else
userExists=true;
}catch(SQLException e){
System.err.println(e);
}
I have checked the database, I know that there is no user id foe example “kunal”- even then the boolean variables evaluates to true. Can we query the ResultSet like that?
Regards,
Kunal Jaggi
Sun Certified Java 2 Programmer
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Facier,
Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks pardner. Hope to see you 'round the Ranch!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ResultSet object returned by executeQuery() is never null. If there are no matching records, an empty ResultSet is returned.
Thus, you need to do something like this instead:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kunal_jaggi,
Thanks for changing your display name. Now, if you could just replace the underscore with a space, our naming policy would be satisfied.
Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34372
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above example, could even be shorter. Checking for an empty result set is fast and easy.
 
Kunal Goel
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lasse and Jeanne, it was something very obvious that I missed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic