Java JDBC MySQL异常:“ResultSet关闭后不允许操作”

时间:2016-02-16 16:25:11

标签: java mysql jdbc

所以我看了好几次这个功能,似乎没有什么不对。我没有将任何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 个答案:

答案 0 :(得分:2)

当你还在循环时,你不应该关闭ResultSet

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