How do I get the size of a java.sql.ResultSet?

Jake picture Jake · Oct 10, 2008 · Viewed 471.3k times · Source

Shouldn't this be a pretty straightforward operation? However, I see there's neither a size() nor length() method.

Answer

finnw picture finnw · Oct 10, 2008

Do a SELECT COUNT(*) FROM ... query instead.

OR

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

In either of the case, you won't have to loop over the entire data.