2016-02-16 121 views
0

所以我查了好幾遍槽功能,沒有什麼看起來錯了。我沒有將任何ResultSet傳遞給函數,所以我真的不知道它有什麼問題。但我仍然遇到錯誤。該代碼工作正常,只是該功能使用了很多次,錯誤在控制檯上看起來不太好。Java JDBC MySQL異常:「ResultSet關閉後不允許操作」

void addItemToBody(String userId, MessageReceivedEvent event, int id) throws HTTP429Exception, DiscordException, MissingPermissionsException{ 
    String sql = "SELECT ItemType FROM items WHERE ID=?"; 
    PreparedStatement state; 
    try { 
     state = Main.conn.prepareStatement(sql); 
     state.setInt(1, id); 
     ResultSet result = state.executeQuery(); 
     while(result.next()){ 
      String type = result.getString("ItemType"); 
      if(type.equals("Head")){ 
       state.executeUpdate("UPDATE body SET headID="+id+" WHERE playerID='"+userId+"'"); 
      } 
      if(type.equals("Chest")){ 
       state.executeUpdate("UPDATE body SET chestID="+id+" WHERE playerID='"+userId+"'"); 
      } 
      if(type.equals("Pants")){ 
       state.executeUpdate("UPDATE body SET pantsID="+id+" WHERE playerID='"+userId+"'"); 
      } 
      if(type.equals("Boots")){ 
       state.executeUpdate("UPDATE body SET bootsID="+id+" WHERE playerID='"+userId+"'"); 
      } 
      if(type.equals("Melee")||type.equals("Magic")||type.equals("Ranged")){ 
       state.executeUpdate("UPDATE body SET weaponID="+id+" WHERE playerID='"+userId+"'"); 
      } 
      sendMessage("Item equipped!",event); 
      result.close(); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 

這是錯誤:

java.sql.SQLException: Operation not allowed after ResultSet closed 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) 
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743) 
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6320) 
at martacus.mart.bot.rpg.InventoryHandler.addItemToBody(InventoryHandler.java:143) 
at martacus.mart.bot.rpg.InventoryHandler.equipItem(InventoryHandler.java:92) 
at martacus.mart.bot.rpg.InventoryHandler.OnMesageEvent(InventoryHandler.java:38) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at sx.blah.discord.handle.EventDispatcher.dispatch(EventDispatcher.java:104) 
at sx.blah.discord.api.internal.DiscordWS.messageCreate(DiscordWS.java:323) 
at sx.blah.discord.api.internal.DiscordWS.onMessage(DiscordWS.java:144) 
at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:312) 
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:368) 
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:157) 
at org.java_websocket.client.WebSocketClient.interruptableRun(WebSocketClient.java:230) 
at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:188) 
at java.lang.Thread.run(Unknown Source) 
+1

@wero是解決當前的問題,正確的 - 但進一步的優化將使所有的更新查詢爲PreparedStatements以及 – SnakeDoc

+0

@SnakeDoc是的,現在我做了,我曾經這樣做,但我現在替換它們。感謝:P – Martacus

回答

2

你不應該關閉ResultSet,而你仍然通過其循環

while(result.next()){ 
    ... 
    result.close(); 
} 
+0

擊敗了我2秒! – SnakeDoc

+1

即使這樣做,在代碼中完成對同一個語句對象的另一個查詢也應該關閉ResultSet。另見http://stackoverflow.com/questions/32671535/operation-not-allowed-after-resultset-closed-mysql-java?rq=1 –

+0

噢謝謝馬克,我完全忘了那個...... – Martacus

相關問題