检索ResultSet中的条目数

Val*_* Ru 1 java sqlite resultset

在SO的一些答案中,建议使用ResultSet函数getRow()来获取查询结果中的条目数.但是,遵循代码

private static int getSizeOfResultSet(ResultSet result) throws SQLException{
    if(result != null){
        int count = 0;
        while(result.next()){
            count++;
        }
        System.out.println("Count of resultset is " + result.getRow() + " and count " + count);
            return result.getRow();
    }else{
        System.out.println("Result is null");
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

正在回报我的事情

Count of resultset is 0 and count 1
Run Code Online (Sandbox Code Playgroud)

确保条目存在时(在JUnit测试用例中).请注意,此项目必须符合Java6标准,因此可能是问题所在.使用该getRow()功能有什么需要考虑的吗?

另外,我尝试过

result.last();
int c = result.getRow();
result.beforeFirst();
return c;
Run Code Online (Sandbox Code Playgroud)

就像这里提到的那样,结果仍然是0.

编辑:我应该提到while(result.next())-loop是用于测试目的.没有循环的行为完全相同(即0).

Sot*_*lis 5

javadoc说

返回当前行号; 如果没有当前行,则为0

在您访问了所有行之后,ResultSet#next()没有当前行.

将您的count值用于行数.或者使用SELECT count(*) ...样式查询.