所以我查了好幾遍槽功能,沒有什麼看起來錯了。我沒有將任何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)
@wero是解決當前的問題,正確的 - 但進一步的優化將使所有的更新查詢爲PreparedStatements以及 – SnakeDoc
@SnakeDoc是的,現在我做了,我曾經這樣做,但我現在替換它們。感謝:P – Martacus