【发布时间】:2018-09-17 18:04:51
【问题描述】:
我遇到了这个问题,我无法以相同的方法执行 executeQuery 和 executeUpdate,它给了我“PSQLException:此 ResultSet 已关闭”。 我试图创建另一个语句和其他 ResultSet 但没有成功。我对您如何帮助我有任何想法,我将不胜感激。谢谢
Statement stmt = pc.getStatement();
try
{
ResultSet rs = stmt.executeQuery("select * from artigos where
artigoc="+s);
while (rs.next())
{
int stock = rs.getInt("stock");
stmt.executeUpdate("update artigos set stock="+newStock+","
+ "vendidos="+newVendidos+" where artigoc="+s);
}
rs.close(); // muito importante depois da consulta!
} catch (Exception e) {
e.printStackTrace();
System.err.println("Problems retrieving data from db...");
}
【问题讨论】:
-
在异常相关的问题中,如果您发布堆栈跟踪会有所帮助
-
仅供参考:您应该在
Statement和ResultSet上都使用try-with-resources。 -
@Andreas 我已经做到了
-
@FilipeCarvalho 如果您已经这样做了,为什么问题中的代码没有这样做?我认为您将常规
try声明与 try-with-resources 声明混淆了。 -
另外,不要使用字符串连接将值插入到 SQL 语句中。使用带有参数标记的
PreparedStatement(?)。
标签: java postgresql jdbc resultset