2012-11-13

Get row count in Java (JDBC)


In previous version fo JDBC, you only can get row count of query by using this method:

ResultSet rs = stmt.executeQuery("SELECT COUNT(*) AS rowCount FROM TestTable");
rs.next();
int count = rs.getInt("rowCount") ;
rs.close() ;

After JDBC 2.0, there is new method called getRow:


ResultSet rs = stmt.executeQuery("SELECT * FROM TestTable");
if (rs.last())//Return false if set is empty
{
     int count = rs.getRow();
     rs.beforeFirst();
     //Do your own access here
}
rs.close() ;

No comments: