Jump to Real's How-to Main page

Get a record count with the SQL Statement

Statement s = con.createStatement();
ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM MyTable");
r.next();
int ResultCount = r.getInt("rowcount") ;
r.close() ;
JDBC 2.0 provides a way to retrieve a rowcount from a ResultSet without having to scan through all the rows or issue a separate SELECT COUNT(*).
Statement s = 
   conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, 
               ResultSet.CONCUR_READ_ONLY);
ResultSet r = 
   s.executeQuery("SELECT * FROM employee WHERE id_emp LIKE '1%'");
r.last();
count = r.getRow();
r.beforeFirst();

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]