0

I've seen a lot of similar questions which try to achieve this by using a while loop to count the entries. But I was hoping that there would be a way similar to this which could work? String SQL = "SELECT Name FROM (table name) TABLE WHERE SCHEMA

    PreparedStatement statement = conn.prepareStatement(SQL);
    ResultSet a = statement.executeQuery();
    System.out.println(a);
Daniel Savage
  • 123
  • 3
  • 13
  • `select count(foo) from bar` + `int numRows = result.getInt(1)` in a separate query would work without much data traffic and without a loop – dly Aug 19 '15 at 11:07
  • possible duplicate of [Get Number of Rows returned by ResultSet in Java](http://stackoverflow.com/questions/8292256/get-number-of-rows-returned-by-resultset-in-java) – Jordi Castilla Aug 19 '15 at 11:09

2 Answers2

1

Maybe you could use count.

SELECT COUNT(column_name) FROM table_name;

Or you could do it using a variable.

int count = 0;
while(rs.next())
  count ++;
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

ResultSet.last() followed by ResultSet.getRow() will give you the row count, but it may not be a good idea as it can mean reading the entire table over the network and throwing away the data

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

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

Community
  • 1
  • 1
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78