Reputation: 725
So I have looked several times trough the function and nothing seems wrong. I don't pass any ResultSets to the function so I really do not get what is wrong with it. But I still get an error. The code works fine, its just that the function is used quite a lot of times and errors do not look nice on the console.
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();
}
}
This is the error:
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)
Upvotes: 0
Views: 683
Reputation: 32980
You shouldn't close the ResultSet
while you are still looping through it
while(result.next()){
...
result.close();
}
Upvotes: 2