无效状态,ResultSet对象已关闭

发布于 2024-12-02 12:14:22 字数 1005 浏览 0 评论 0原文

我正在运行代码,但是收到“无效状态,ResultSet 对象已关闭”。错误。是什么导致了错误?

try{
    query = "SELECT * FROM BUNDLE_TEMP "
                  + "MINUS "
                  + "SELECT * FROM BUNDLE";

            rs = stmt.executeQuery(query);

            while (rs.next()){
                String bundle = rs.getString("BUNDLE");
                String week = rs.getString("WEEK");
                String sched_dt = rs.getString("SCHED_DT").replace(" 00:00:00.0", "");
                String dropper_id = rs.getString("DROPPER_ID");


                query = "INSERT INTO BUNDLE "
                            + "VALUES ('"
                                + bundle+"','"
                                + week+"','"
                                + sched_dt+"','"
                                + dropper_id+"')";

                stmt.executeUpdate(query);
            }
        }catch(Exception e){
            System.out.println("Error while trying to insert into BUNDLE\n"+query+"\n"+ e);
        }

I am running the code, however am getting a "Invalid state, the ResultSet object is closed." error. What is causing the error?

try{
    query = "SELECT * FROM BUNDLE_TEMP "
                  + "MINUS "
                  + "SELECT * FROM BUNDLE";

            rs = stmt.executeQuery(query);

            while (rs.next()){
                String bundle = rs.getString("BUNDLE");
                String week = rs.getString("WEEK");
                String sched_dt = rs.getString("SCHED_DT").replace(" 00:00:00.0", "");
                String dropper_id = rs.getString("DROPPER_ID");


                query = "INSERT INTO BUNDLE "
                            + "VALUES ('"
                                + bundle+"','"
                                + week+"','"
                                + sched_dt+"','"
                                + dropper_id+"')";

                stmt.executeUpdate(query);
            }
        }catch(Exception e){
            System.out.println("Error while trying to insert into BUNDLE\n"+query+"\n"+ e);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-12-09 12:14:22

您无法对当前使用 ResultSet 进行迭代的同一个 Statement 执行另一个 SQL 查询。执行此操作将关闭之前打开的游标(您的 SELECT 查询或 ResultSet):

引用 Statement 的 API 文档

默认情况下,每个Statement对象只能打开一个ResultSet对象
同时。因此,如果读取一个 ResultSet 对象是
与另一个的读取交错,每个都必须已生成
由不同的 Statement 对象。 Statement中的所有执行方法
接口隐式关闭语句的当前 ResultSet 对象,如果
打开一个已存在。

从您的 Connection 创建另一个 Statement 实例,我们将其称为 updateStmt 并在该实例上调用 executeUpdate()

另外,请查看您的更新的Prepared statements,它可能会更高效、更安全。

You cannot execute another SQL query on the same Statement you're currently iterating over with a ResultSet. Doing this will close the previously open cursor (your SELECT query resp. ResultSet):

To quote the API docs for Statement:

By default, only one ResultSet object per Statement object can be open
at the same time. Therefore, if the reading of one ResultSet object is
interleaved with the reading of another, each must have been generated
by different Statement objects. All execution methods in the Statement
interface implicitly close a statment's current ResultSet object if an
open one exists.

Create another Statement instance from your Connection, let's call it updateStmt and executeUpdate() on that one.

Also, look into Prepared Statements for your update, it will probably be more performant and secure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文