Alex Rivera | Logout

Getting the count of rows in a Java resultset

Asked 2010-06-16T11:11:05.620
29

Does anyone know a better way of getting the number of rows in a Java resultset returned from a MySQL database? The resultset returned is not going to be the total number of rows read from the database so I don't think I can use SQL's COUNT aggregate function.

public static int getResultSetRowCount(ResultSet resultSet) {
    int size = 0;
    try {
        resultSet.last();
        size = resultSet.getRow();
        resultSet.beforeFirst();
    }
    catch(Exception ex) {
        return 0;
    }
    return size;
}
Edit
Report

1 Answer

2

You can always use SELECT COUNT() with the same exact conditions, before making the actual SELECT.

answered 2010-06-16T11:32:20.323

Your Answer