无效状态,ResultSet对象已关闭 [英] Invalid state, the ResultSet object is closed

查看:201
本文介绍了无效状态,ResultSet对象已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行代码,但是得到的是无效状态,ResultSet对象已关闭".错误.是什么原因导致错误?

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

推荐答案

您无法在当前使用ResultSet进行迭代的同一Statement上执行另一个SQL查询.这样做将关闭先前打开的游标(您的SELECT查询和ResultSet):

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):

引用声明的API文档:

默认情况下,每个Statement对象只能打开一个ResultSet对象 同时.因此,如果读取一个ResultSet对象是 与另一个的阅读交织在一起,每个必须已经生成 通过不同的Statement对象.语句中的所有执行方法 如果隐式关闭了一个语句的当前ResultSet对象,则该接口隐式关闭 打开一个存在.

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.

从您的Connection创建另一个Statement实例,在该实例上将其称为updateStmtexecuteUpdate().

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

此外,请查看 Prepared Statements 进行更新,它可能会更多性能和安全性.

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

这篇关于无效状态,ResultSet对象已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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