Coder Perfect

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

Problem

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.

OR

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 rs.next() below will move on, missing the first element
}
while (rs.next()) {
  // 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.

Example

Let’s imagine you have the following question:

select MYBOOL,MYINT,MYCHAR,MYSMALLINT,MYVARCHAR
from MYTABLE
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,
                                         ResultSet.CONCUR_READ_ONLY);
String from_where="FROM myTable WHERE ...blahblah... ";
//h4x
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 "
                           +from_where
                           +"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 "
                           +from_where);

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

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

Answered by Unai Vivi

Solution #4

int i = 0;
while(rs.next()) {
    i++;
}

Answered by bhaskar

Solution #5

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

if(rs.last()){
    rowCount = rs.getRow(); 
    rs.beforeFirst();
}

:

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

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

the solution is:

stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY); 

Answered by Dan

Post is based on https://stackoverflow.com/questions/192078/how-do-i-get-the-size-of-a-java-sql-resultset