java ResultSet 关闭后不允许操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10933399/
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-10-31 03:08:29  来源:igfitidea点击:

Operation not allowed after ResultSet closed

javasqlselectresultset

提问by user1442387

Allright been trying to figure this out the last 2 days.

好吧,过去两天一直在努力解决这个问题。

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

This is the query method.

这是查询方法。

    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;
    }

How can I fix this error?

我该如何解决这个错误?

回答by NPE

It's hard to be sure just from the code you've posted, but I suspect that the ResultSetis inadvertently getting closed (or stmis getting reused) inside the body of the whileloop. This would trigger the exception at the start of the following iteration.

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

Additionally, you need to make sure there are no other threads in your application that could potentially be using the same DB connection or stmobject.

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

回答by iozee

IMHO, you should do everything you need with your ResultSet before you close your connection.

恕我直言,在关闭连接之前,您应该对 ResultSet 执行所需的一切操作。

回答by srini.venigalla

there are few things you need to fix. Opening a connection, running a query to get the rs, closing it, and closing the connection all should be done in the same function scope as far as possible. from your code, you seem to use the "con" variable as a global variable, which could potentially cause a problem. you are not closing the stm object. or the rs object. this code does not run for too long, even if it has no errors. Your code should be like this:

您需要解决的问题很少。打开连接、运行查询以获取 rs、关闭它和关闭连接都应尽可能在相同的函数范围内完成。从您的代码中,您似乎将“con”变量用作全局变量,这可能会导致问题。您没有关闭 stm 对象。或 rs 对象。这段代码不会运行太长时间,即使它没有错误。你的代码应该是这样的:

if (stringUtils.isBlank(sql)){
     throw new IllegalArgumentsException ("SQL statement is required");
}
Connection con = null;
PreparedStatement ps =null;
Resultset rs = null;
try{
         con = getConnection();
         ps = con.preparestatement(sql);
         rs = ps.executeQuery();
         processResults(rs);
         close(rs);
         close(ps);
         close(con);
}catch (Execption e){
        log.Exception ("Error in: {}", sql, e);
        throw new RuntimeException (e);
}finally{
        close(rs);
        close(ps);
        close(con);
}

回答by Saurabh Kachhia

use another Statement object in inner loop Like

在内循环中使用另一个 Statement 对象

Statement st,st1;

st=con.createStatement();
st1=con.createStatement();

//in Inner loop
while(<<your code>>)
{
   st1.executeQuery(<<your query>>);
}

回答by Zee

I know this is a few years late, but I've found that synchronizing the db methods usually get rid of this problem.

我知道这晚了几年,但我发现同步 db 方法通常可以解决这个问题。