I had exactly this problem a while ago, when I needed to check whether
a given ResultSet was empty, and if not, how many rows did it have. I'm
rather new to JDBC and SQL, but since I couldn't find any appropriate
methods, I wrote the following class (swedish comments removed :-) :

import java.sql.*;

public final class Utilities {

     private Utilities() { }

     public static boolean isEmpty(ResultSet rs) throws SQLException {
        boolean result = !rs.next();
                rs.beforeFirst();
                return result;
     }

     public static int getRowCount(ResultSet rs) throws SQLException {
                int i = 0;
                while (rs.next()) i++;
                rs.beforeFirst();
                return i;
     }

}

Then just use it like this:

        if (Utilities.isEmpty(rs)) {
                // empty
        }

or, you could:

        if (Utilities.getRowCount(rs) == 0) {
                // empty
        }

Hope this helps.

[ Matthias Carlsson ]
[ Programmer (Java, CGI/Perl, Javascript, HTML) ] [ Web Designer ]
[ E-Mail : [EMAIL PROTECTED] ] [ ICQ: 1430647 ]

> -----Ursprungligt meddelande-----
> Fr�n: A mailing list for discussion about Sun Microsystem's Java Servlet
> API Technology. [mailto:[EMAIL PROTECTED]]F�r James Lee
> Skickat: den 24 augusti 2000 18:28
> Till: [EMAIL PROTECTED]
> �mne: how to check ResultSet is null or not?
>
>
> Hi All,
>
> I want to display a message when the ResultSet is empty from the database.
> But when I try to check ResultSet  by using the following methods like:
>   if ( rset == null ) {
>      //  display the message
>   } else {
>      //  display the results
>   }
> it seems not working.  I also tried to use
>   if ( !rset.next() )    and  if (!rset.first())  but none of them seems
> work.
>
> I am using Weblogic JDBC2.0 connection pool.  It works fine if I do not
> check the ResultSet and just display them.  Only thing is I got a
> blank page
> if no results.
>
> Any suggestions?
>
> James Lee
>
> __________________________________________________________________
> _________
> To unsubscribe, send email to [EMAIL PROTECTED] and include
> in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to