Solved

How to find out if there a Database ResultSet contains results

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 88
xampp tool 12 56
Java exception bubble up 2 21
How to configure empty element in XML Document parser? 15 36
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 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