Java 使用HSQLDB时如何获取Resultset返回的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19533991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:07:32  来源:igfitidea点击:

How to get number of rows returned by Resultset while using HSQLDB?

javahsqldbresultsetrowcount

提问by Learner

I am getting exception feature not supported while getting the total no of rows using ResultSet.last() function. I tried using the hsqldb specific connection and ResultSet class but no success.

使用 ResultSet.last() 函数获取总行数时,我得到了不支持的异常功能。我尝试使用特定于 hsqldb 的连接和 ResultSet 类,但没有成功。

Can some one guide any way to get the no of rows from result set except looping through all the rows.

除了循环遍历所有行之外,有人可以指导任何方式从结果集中获取行数。

the code snippet used for getting no of rows is given below:

用于获取行数的代码片段如下:

rs.last();
int total = rs.getRow();
System.out.println("total no of rows in stu are "+total);
rs.beforeFirst()

采纳答案by Prateek

After having look at HSQLDBapi ,it can be done same as for ORACLEwhich is shown below

查看HSQLDBapi 后,可以按照ORACLE如下所示进行操作

String URL = "jdbc:oracle:thin:@ip:port:sid";
String USER = "test";
String PASS = "test";
String query = "Select * from mytable";
try {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection con = DriverManager.getConnection(URL, USER, PASS);
    Statement stmt = con.createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = stmt.executeQuery(query);
    if (rs.next()) {
        rs.last();
        System.out.println("total rows is : " + rs.getRow());
    } else {
        System.out.println("No Data");
    }
} catch (Exception e) {
    e.printStackTrace();
}