运行多个选择查询apache derby时出错

时间:2016-02-05 21:29:32

标签: java derby

我无法使用多个选择查询运行下面的代码。我可以单独运行下面的查询,但我想一起运行并将结果存储在ArrayList中。我得到的错误是java.sql.SQLException: ResultSet not open. Operation 'next' not permitted. Verify that autocommit is off.

有什么建议吗?或者有更好的方法吗?感谢

public ArrayList<String> getTotalCountBasicQueries() {

        ArrayList<String> totalCount = new ArrayList();
            Statement stmt = null;

            stmt = conn.createStatement();
            conn.setAutoCommit(false);

            try {

                String q1 = "select count query";
                String q2 = "select count query2";
                String q3 = "select count query3 ";

                ResultSet rs = stmt.executeQuery(q1);
                ResultSet rs2 = stmt.executeQuery(q2);
                ResultSet rs3 = stmt.executeQuery(q3);

                while (rs.next()) {
                    totalBasicCount.add(rs.getString(1));
                }

                while (rs2.next()) {
                    totalCount.add(rs2.getString(1));
                }
                while (rs3.next()) {
                    totalCount.add(rs3.getString(1));
                }


                rs.close();
                rs2.close();
                rs3.close();
                stmt.close();
            } catch (Throwable e) {
                System.out.println("Table fetch failed or result data failed");
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                        System.out.println("Could not close query");
                    } catch (SQLException ex) {
                        System.out.println("Could not close query");
                    }
                }

                return totalBasicCount;

            }
        }
    }

1 个答案:

答案 0 :(得分:1)

请参阅ResultSet的javadoc:

  

当Statement对象时,ResultSet对象自动关闭   生成它的是关闭,重新执行或用于检索下一个   由多个结果序列产生。

根据规范,您不能为唯一语句打开多个结果集。但是,有些jdbc驱动程序允许这个

尝试:

ResultSet rs = stmt.executeQuery(q1);
while (rs.next()) {
   totalBasicCount.add(rs.getString(1));
}
ResultSet rs2 = stmt.executeQuery(q2);
while (rs2.next()) {
   totalCount.add(rs2.getString(1));
}
ResultSet rs3 = stmt.executeQuery(q3);
while (rs3.next()) {
   totalCount.add(rs3.getString(1));
}