2010-08-05 93 views
0

我想从结果集中的数据库检索结果。不过,我想执行结果集中每个条目的更新查询,但我得到一个异常。 这是我的代码结果集关闭异常

try { 
     Statement statement = sqlconnection.conn.createStatement(); 
     query = "select * from reminders where year<= "+ syear +" and month<=" + smonth +" and date<"+ sday +" and reminded like 'false';"; 
     rs= statement.executeQuery(query); 
     while (rs.next()){ 
      id=rs.getInt("sno"); 
      String reminder = rs.getString("remind"); 
      JOptionPane.showMessageDialog(null, reminder); 
      statement.executeUpdate("update reminders set reminded='true' where sno="+id+";"); 
     } 

能ANY1告诉我这样做的更好的办法?我对编程非常陌生。因此,向我展示如何将会非常有帮助。 谢谢

+0

你在哪一行得到错误? – KeatsPeeks 2010-08-05 08:21:56

+1

不是你的问题的答案......但如果这是一个生产代码,我看到你的代码中的一个非常大的SQL注入问题......不要使用字符串连接构建SQL squeries ...使用参数化查询insted。 – 2010-08-05 08:23:54

+0

@国王:虽然通常是真的,但我会假设所有变量都是数字。 @Samuel_xL:第二次打电话给re.next(),我敢打赌 – Nicolas78 2010-08-05 08:26:57

回答

2

当您尝试使用它执行更新时,您仍在循环播放statement的结果。我会尝试使用第二个Statement对象进行更新。

0

您的ResultSet不可更新。

Statement statement = sqlconnection.conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);