0

I have written a query which retrieves data from a table and then I am checking the number of rows in the resultset using getRow() method. This is printing 0,however my table contains multiple row column data in it.

I checked previous questions here and found one much similar here. But unlike what is mentioned in that I do not have any permissions defined and connection to the database is well established. It is a very simple query returning updatable resultset as following:

if(temp == null) {
                query = "SELECT * FROM altemp1";
                pstmt = cn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                temp = pstmt.executeQuery();
                System.out.println("TEMP LENGTH : "+temp.getRow());
            }

temp is the ResultSet and altemp1 is the table in DB. The check above if temp is null? is for some logical purpose.

Community
  • 1
  • 1
Harshvardhan Solanki
  • 647
  • 2
  • 11
  • 32
  • The `getRow` method doesn't return the number of rows in the resultset; it returns the current row number. In the example code, the query (prepared statement) is executed, but no rows are fetched from the resultset, so the current row number from the resultset is 0. – spencer7593 Dec 30 '15 at 06:45
  • http://stackoverflow.com/questions/192078/how-do-i-get-the-size-of-a-java-sql-resultset – Somnath Musib Dec 30 '15 at 06:54

2 Answers2

0

You need to call ResultSet.next() and check the return code to fetch each successive row, including the first, and you then need to call various other methods to get the columns, one of which is not getRow(). See the Javadoc.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

According to the JavaDoc (https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getRow%28%29):

[...getRow()] retrieves the current row number. The first row is number 1, the second number 2, and so on.

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34