无效状态,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);
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法对当前使用
ResultSet
进行迭代的同一个Statement
执行另一个 SQL 查询。执行此操作将关闭之前打开的游标(您的SELECT
查询或ResultSet
):引用 Statement 的 API 文档:
从您的
Connection
创建另一个Statement
实例,我们将其称为updateStmt
并在该实例上调用executeUpdate()
。另外,请查看您的更新的Prepared statements,它可能会更高效、更安全。
You cannot execute another SQL query on the same
Statement
you're currently iterating over with aResultSet
. Doing this will close the previously open cursor (yourSELECT
query resp.ResultSet
):To quote the API docs for Statement:
Create another
Statement
instance from yourConnection
, let's call itupdateStmt
andexecuteUpdate()
on that one.Also, look into Prepared Statements for your update, it will probably be more performant and secure.