GeeCON Prague 2014*
The moose likes JDBC and the fly likes Help with empty result set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Help with empty result set" Watch "Help with empty result set" New topic
Author

Help with empty result set

MATT JACKSON
Greenhorn

Joined: Nov 29, 2000
Posts: 5
I have a servlet that accesses a MySQL DB. What it does is scan the input from a JSP to see if the entry being added already exixts. It does this by querying the DB for an ID number. The query is: SELECT idNum FROM students WHERE idNum = inputNum;
The problem i have is that if the query returns nothing, I can call rs.next() but if I call rs.getString(1) to check if the string is null I get an SQLException thrown.
HELP!!!
thanks in advance
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Matt,
If your driver implementation is correct, then you shouldn't run into this problem. When you execute the query, and there are no rows in the database matching your where clause, then your ResultSet should be empty. In this case, ResultSet.next() should return false.
For example,

If your code falls inside the while() loop, then the resultset is not empty.
-Peter
[This message has been edited by Peter Tran (edited January 16, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You can call rs.next() but it will return false if there are no rows in the resultSet.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
MATT JACKSON
Greenhorn

Joined: Nov 29, 2000
Posts: 5
Thanks alot to both replies. I got it working now. That helped alot.
 
GeeCON Prague 2014
 
subject: Help with empty result set