目标:获取Sql查询结果的行数,也就是java.sql.ResultSet的行数
方法1:
Do a SELECT COUNT(*) FROM … query instead.
方法2:
int size =0;
if (rs != null)
{
rs.last(); // moves cursor to the last row
size = rs.getRow(); // get row id
}
不管哪种方式,都不需要遍历所有数据,也就是说,时间复杂度不是O(n).
引自https://stackoverflow.com/questions/192078/how-do-i-get-the-size-of-a-java-sql-resultset