解决java - Invalid state, the ResultSet object is closed

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.

不能对当前使用ResultSet迭代的同一语句执行另一个SQL查询。这样做将关闭先前打开的光标(选择查询resp)。结果集):

引用API文档进行声明:

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

从连接中创建另一个语句实例,在该实例上调用updatesmt和executeupdate()。

另外,查看准备好的更新语句,它可能会更高效、更安全。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值