Solved

How to find out if there a Database ResultSet contains results

Posted on 1998-07-31
3
168 Views
Last Modified: 2010-03-30
I'm developing a servlet and I need to test whether there are any results in the ResultSet returned from my database query, if there aren't any I want to display an html page which indicates this.
What is the method to determine if there are results, I don't want to use 'next()' as this moves the resultset counter on one.

Please send me the code.

Thanks

Mel
0
Comment
Question by:help_me
  • 2
3 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1229589
I am afraid you have to call next() and cache the row values if the set is not empty.
0
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1229590
There's no other way to find out if the ResultSet is empty as to call the next() method.  Here's what you can do:

ResultSet = db.executeQuery(...);
if(rs == null) {
    //Error in the query string
}
if(!rs.next()) {
    //ResultSet is empty - do whatever you need
} else {
    do {
        //Do whatever you need with each row
    } while(rs.next());
}

This way you don't loose the first record by using next() to figure out whether the recordset is empty.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1229591
Statement.executeQuery() never returns null, a ResultSet object (possibly empty) is always returned.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now