Solved

How to find out if there a Database ResultSet contains results

Posted on 1998-07-31
3
174 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 55
Maven Project: Hibernate Dependencies Conflict 10 24
Which non-HTML GUI front end to use with Java? 3 22
Problem to Alipay 10 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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