The ResultSet API in JDBC does not have a way to get the number of records directly, and now describes several:
Method one: Using ResultSet's GetRow method to get the total number of resultset
ResultSet rs = ps.executequery (SQL);
Rs.last (); Move to the last row
int rowCount = Rs.getrow ();//Gets the current line number, which is the number of records
Rs.beforefirst ();
If you want to use the result set again, move the pointer to the initialized position
Method Two: Using the elements of cyclic resultset to get the total number of resultset
ResultSet rs = ps.executequery (SQL);
int rowCount = 0; while (Rs.next ())
{
rowcount++;
}
Method Three: Use the Count function in the SQL statement to get the total number of resultset
String sql = "SELECT COUNT (*) record from tablename";
ResultSet rs = ps.executequery (SQL);
int rowCount = 0;
if (Rs.next ())
{
Rowcount=rs.getint ("Record");
}
It is true that the JDK API does not give us a very explicit number of records on the resultset result set, but we still know through the method that there are other ways to work around this. So, we often use the JDK, perhaps you want a feature, although in the API may not find the corresponding method, but often can be implemented in other ways, although sometimes such a workaround may not be efficient, But it is also often our own to achieve more than it may be more than a variety of conversion to better, so must learn more, more research API, whether it is the JDK, or other technology, in fact, as long as you are built on its platform, your vast majority of the needs can be through its existing to sublimate. Anyway is to have more than one kind of thinking, is standing on the shoulders of others to learn and sublimation, I believe that we use the JDK, open source framework and Toolkit development level is very high.
Several ways to get the number of records to RS by resultset