ResultSet关闭后不允许操作

use*_*387 6 java sql select resultset

好吧,在过去的两天里,他一直试图解决这个问题.

Statement statement = con.createStatement();
                        String query = "SELECT * FROM sell";
                        ResultSet rs = query(query);
                        while (rs.next()){//<--- I get there operation error here
Run Code Online (Sandbox Code Playgroud)

这是查询方法.

    public static ResultSet query(String s) throws SQLException {
        try {
            if (s.toLowerCase().startsWith("select")) {
                if(stm == null) {
                    createConnection();
                }
                ResultSet rs = stm.executeQuery(s);
                return rs;
            } else {
                if(stm == null) {
                    createConnection();
                }
                stm.executeUpdate(s);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            con = null;
            stm = null;
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

NPE*_*NPE 5

很难确定你发布的代码,但我怀疑循环体内ResultSet无意中关闭(或被stm重用).这将在下一次迭代开始时触发异常.while

此外,您需要确保应用程序中没有可能使用相同数据库连接或stm对象的其他线程.