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);
2
  • select count(foo) from bar + int numRows = result.getInt(1) in a separate query would work without much data traffic and without a loop Commented Aug 19, 2015 at 11:07
  • possible duplicate of Get Number of Rows returned by ResultSet in Java Commented Aug 19, 2015 at 11:09

2 Answers 2

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 ++;
Sign up to request clarification or add additional context in comments.

1 Comment

he will have to query again to get the data
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.