ResultSet 关闭后不允许操作 [英] Operation not allowed after ResultSet closed

查看:54
本文介绍了ResultSet 关闭后不允许操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是查询方法.

    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?

推荐答案

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

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

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

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

这篇关于ResultSet 关闭后不允许操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆