2013-04-13 87 views
0

,當我有這個代碼的問題:JDBC例外「不允許操作的ResultSet關閉後,」迭代的ResultSet

ResultSet dane = statement.executeQuery("SELECT * FROM ProWarns WHERE blahblah = '" + cel + "'"); 

while (dane.next()) { 
    // some code (only get some string from result) 
    if (TimeEnd <= EndTime) { 
     statement.executeUpdate(
      "DELETE FROM ProWarns WHERE id = '" + id + "'"); //and error 

     statement.executeUpdate(
      "UPDATE ProWarnsPlayers SET num = '" + NewV + "'" WHERE Pl = '"+ tar + "'"); 
    } 
} 

錯誤:java.sql.SQLException中:不允許操作的ResultSet關閉後。錯誤在哪裏,我該如何解決它?

PS:

我包括我的DB類的Pastebin,以防其有幫助。

回答

1

A Statement對象緩存其結果集,因此當您在for循環中執行其他操作時,原始結果集會「重置」。這會導致發生錯誤,當您撥打dane.next時。來自Javadoc

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.

選項?使用另一個Statement對象來執行內部查詢。

+0

hyym。那不會創建第二個線程? – user2250333

+1

不,你不打算創建第二個線程,只是創建第二個語句對象。 – Perception

相關問題