微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 状态无效,ResultSet对象关闭

我正在运行代码,但是得到“无效状态,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);
        }

解决方法:

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

引用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.

从Connection创建另一个Statement实例,让我们在那个上调用updateStmt和executeUpdate().

另外,请查看Prepared Statements获取更新,它可能会更高效,更安全.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐