Coder Perfect

How do I find out how big a java.sql.ResultSet is?


Isn’t this supposed to be a rather simple procedure? However, I don’t see any size() or length() methods.

Asked by Jake

Solution #1

Instead, use a SELECT COUNT(*) FROM… query.


int size =0;
if (rs != null) 
  rs.last();    // moves cursor to the last row
  size = rs.getRow(); // get row id 

You won’t have to loop through all of the data in any instance.

Answered by finnw

Solution #2

ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
  rowcount = rs.getRow();
  rs.beforeFirst(); // not rs.first() because the below will move on, missing the first element
while ( {
  // do your standard per row stuff

Answered by JeeBee

Solution #3

If you have a ResultSet.TYPE FORWARD ONLY, you should leave it that way (rather than switching to a ResultSet.TYPE SCROLL INSENSITIVE or ResultSet.TYPE SCROLL INSENSITIVE in order to use.last()).

I propose a really elegant and efficient hack: at the start, create a first bogus/phony row with the number of rows.


Let’s imagine you have the following question:

where ...blahblah...

and your final product appears to be

true    65537 "Hey" -32768 "The quick brown fox"
false  123456 "Sup"    300 "The lazy dog"
false -123123 "Yo"       0 "Go ahead and jump"
false       3 "EVH"    456 "Might as well jump"
[1000 total rows]

Simply change your code to look like this:

Statement s=myConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
String from_where="FROM myTable WHERE ...blahblah... ";
ResultSet rs=s.executeQuery("select count(*)as RECORDCOUNT,"
                           +       "cast(null as boolean)as MYBOOL,"
                           +       "cast(null as int)as MYINT,"
                           +       "cast(null as char(1))as MYCHAR,"
                           +       "cast(null as smallint)as MYSMALLINT,"
                           +       "cast(null as varchar(1))as MYVARCHAR "
                           +"UNION ALL "//the "ALL" part prevents internal re-sorting to prevent duplicates (and we do not want that)
                           +"select cast(null as int)as RECORDCOUNT,"
                           +       "MYBOOL,MYINT,MYCHAR,MYSMALLINT,MYVARCHAR "

The result of your query will now look like this:

1000 null     null null    null null
null true    65537 "Hey" -32768 "The quick brown fox"
null false  123456 "Sup"    300 "The lazy dog"
null false -123123 "Yo"       0 "Go ahead and jump"
null false       3 "EVH"    456 "Might as well jump"
[1001 total rows]

So all you have to do now is

    System.out.println("Recordcount: "+rs.getInt("RECORDCOUNT"));//hack: first record contains the record count
    //do your stuff

Answered by Unai Vivi

Solution #4

int i = 0;
while( {

Answered by bhaskar

Solution #5

When I used rs.last, I got an exception ()

    rowCount = rs.getRow(); 


java.sql.SQLException: Invalid operation for forward only resultset

It’s because it’s a ResultSet by default. Only can be used with TYPE FORWARD ONLY ()

the solution is:


Answered by Dan

Post is based on