Reputation: 5052
In a few answers here on SO there is recommended to use ResultSet
's function getRow()
in order to get the number of entries in a query result. However, following code
private static int getSizeOfResultSet(ResultSet result) throws SQLException{
if(result != null){
int count = 0;
while(result.next()){
count++;
}
System.out.println("Count of resultset is " + result.getRow() + " and count " + count);
return result.getRow();
}else{
System.out.println("Result is null");
return 0;
}
}
is returning me things like
Count of resultset is 0 and count 1
when there is assured that entries are existing (in a JUnit test case). Note that this project has to be in Java6-standard, so maybe this is the issue. Is there anything to consider using the getRow()
function?
Additionally, I tried with
result.last();
int c = result.getRow();
result.beforeFirst();
return c;
like mentioned here, but the result is still 0.
EDIT: I should mention that the while(result.next())
-loop is for testing purpose. The behaviour without the loop is exactly the same (i.e. 0
).
Upvotes: 1
Views: 174
Reputation: 279960
The javadoc states
returns the current row number; 0 if there is no current row
After you've visited all the rows with ResultSet#next()
there is no current row.
Use your count
value for the number of rows. Or use a SELECT count(*) ...
style query.
Upvotes: 5