This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Quering the ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Quering the ResultSet" Watch "Quering the ResultSet" New topic
Author

Quering the ResultSet

kunal Jaggi
Greenhorn

Joined: Jun 17, 2003
Posts: 1
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

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

The above example, could even be shorter. Checking for an empty result set is fast and easy.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kunal Goel
Greenhorn

Joined: Sep 08, 2002
Posts: 16
Thanks Lasse and Jeanne, it was something very obvious that I missed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quering the ResultSet
 
Similar Threads
Help me on userlogin
SQL Exception: Cursor state not valid
Row information from ResultSet
Closing Statements
Encoding Conversion using JDBC