Tuesday, June 9, 2009

Get number of rows in resultset

You can extract the data from the resultset from the top and rs.last() will move you to the end of the resultset.

Then, the method rs.getRow() get the row number of the last row and also shows you the number of rows in the table.

For example:

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
String query = "select [table_field] from [table_name]";
stmt = conn.createStatement();

rs = stmt.executeQuery(query);
while (rs.next()) {
String id = rs.getString(1);
int rowCount = rs.getRow();
System.out.println("Number of Rows=" + rowCount);
} catch (Exception e) {
} finally {
try {
} catch (SQLException e) {}

ref: http://www.roseindia.net/java/java-get-example/number-rows-resul.shtml

No comments:

Post a Comment