Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to get row count in JDBC?

843854Apr 7 2003 — edited Sep 24 2007
hi all,
i am using JDBC. When i get a ResultSet from the method Statement.executeQuery(sql), i'd like to know how many rows there are in the result set. I searched in the JDBC APIs documents, tried to find a method such as ResultSet.getRowCount(), but i failed. What i do now is like the following:
String sql = "SELECT name FROM myTable";
ResultSet set = stmt.executeQuery(sql);
int rowCount = 0;
if (set.last()==true) {
  rowCount = set.getRow();
}
String[] names = new String[rowCount];
set.first();
for (int i=0; i<rowCount; i++) {
  names[i] = set.getString("name");
  set.next();
}
My method is not pretty at all. I'd like to know how to reimplement the code above in a more graceful way. thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 22 2007
Added on Apr 7 2003
9 comments
634 views