How get the number of rows count by ResultSet in Java [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-11 23:19:53

问题


I want to get the row count of a ResultSet.

ResultSet rs;
Statement s;
rs=s.executeQuery("SELECT * FROM mytable");

//rs.getRowCount; ???

回答1:


You can go to the last row of the resultset and get the row number like this:

resultSet.last()
int count = resultSet.getRow()

But this is inefficient because it has to read all the table data. Better to execute the query:

SELECT COUNT(*) FROM mytable



回答2:


I think we can get count as given below:

int size = 0;
try {
    while(rs.next()){
        size++;
    }
}
catch(Exception ex) { }

or we can use

ResultSet.getRow()


来源:https://stackoverflow.com/questions/25351452/how-get-the-number-of-rows-count-by-resultset-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!