0

I want to get the row count of a ResultSet.

ResultSet rs;
Statement s;
rs=s.executeQuery("SELECT * FROM mytable");

//rs.getRowCount; ???
user3789426
  • 13
  • 1
  • 7
ikk
  • 3
  • 1
  • 1
  • 3
  • You can also try looking at these similar articles: http://stackoverflow.com/questions/7886462/get-row-count-of-a-resultset-in-java http://stackoverflow.com/questions/8292256/get-number-of-rows-returned-by-resultset-in-java – Zexus Aug 17 '14 at 17:02

2 Answers2

3

You can go to the last row of the resultset and get the row number like this:

resultSet.last()
int count = resultSet.getRow()

But this is inefficient because it has to read all the table data. Better to execute the query:

SELECT COUNT(*) FROM mytable
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
0

I think we can get count as given below:

int size = 0;
try {
    while(rs.next()){
        size++;
    }
}
catch(Exception ex) { }

or we can use

ResultSet.getRow()