无效状态,ResultSet对象已关闭

编程入门 行业动态 更新时间:2024-10-22 02:58:55
本文介绍了无效状态,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实例,在该实例上将其称为updateStmt和executeUpdate().

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对象已关闭

本文发布于:2023-10-18 10:56:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504031.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   状态   ResultSet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!