Solved

How to find out if there a Database ResultSet contains results

Posted on 1998-07-31
3
156 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
Comment Utility
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
Comment Utility
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
Comment Utility
Statement.executeQuery() never returns null, a ResultSet object (possibly empty) is always returned.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

9 Experts available now in Live!

Get 1:1 Help Now