知道如何获得总行数

时间:2015-08-10 09:20:42

标签: java database sqlite jdbc

我想获取sqlite数据库中的总行数,我提到了一些帖子,我写了下面的代码,但在运行时我收到了xception in thread "main" java.sql.SQLException: SQLite only supports TYPE_FORWARD_ONLY cursors

我将参数设为

stmt = conn.createStatement(
            ResultSet.TYPE_FORWARD_ONLY, 
            ResultSet.CONCUR_READ_ONLY);

但是在运行时我收到的行数等于0,尽管数据库中有行。

请告诉我如何正确计算总行数?

private final String selectRowCount = "select COUNT(*) from 
"+TABLE_NAME+";";
...
...
public int getRowCount() throws SQLException, ClassNotFoundException {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(
            ResultSet.TYPE_SCROLL_SENSITIVE, 
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resSet = stmt.executeQuery(this.selectRowCount);

    int size = 0;
    try {
        resSet.last();
        size = resSet.getRow();
        resSet.beforeFirst();
    }
    catch(Exception ex) {
        resSet.close();
        stmt.close();
        conn.close();

        Log.d(TAG, "", "RowCount: "+size);
        return 0;
    }

    resSet.close();
    stmt.close();
    conn.close();

    Log.d(TAG, "", "RowCount: "+size);
    return size;
}

0 个答案:

没有答案