SQLException结果集结束后

时间:2016-12-30 07:35:53

标签: java mysql minecraft

当我运行这部分代码时,它给了我一个SQLException。所有的MySQL信息都是正确的,我在其他类中使用的代码也是正确的,但只是查询其他内容并且它工作得很好。

这是堆栈跟踪

[23:24:06 WARN]: java.sql.SQLException: After end of result set
[23:24:06 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
[23:24:06 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
[23:24:06 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
[23:24:06 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
[23:24:06 WARN]:        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
[23:24:06 WARN]:        at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2472)
[23:24:06 WARN]:        at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2583)
[23:24:06 WARN]:        at me.Austin.MT.RecentTickets.recentTickets(RecentTickets.java:47)
[23:24:06 WARN]:        at me.Austin.MT.GUIs.AdminGUIManager.onInvClick(AdminGUIManager.java:94)
[23:24:06 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[23:24:06 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[23:24:06 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[23:24:06 WARN]:        at java.lang.reflect.Method.invoke(Unknown Source)
[23:24:06 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
[23:24:06 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
[23:24:06 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502)
[23:24:06 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1844)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13)
[23:24:06 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
[23:24:06 WARN]:        at java.util.concurrent.FutureTask.run(Unknown Source)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:739)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675)
[23:24:06 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574)
[23:24:06 WARN]:        at java.lang.Thread.run(Unknown Source)

以下是代码

public class RecentTickets {

public static HashMap<Integer, String> rTickets = new HashMap<>();
/*
 * TODO: Return the most 5 recent
 * 
 * 
 */

/**
 * RecentTickers Class - Returns the recent tickets
 *
 * @param p Player that runs the command
 * @return The 5 most recent tickets
 * @throws SQLException Just in case MySQL wants to be a dick
 */
public static HashMap<Integer, String> recentTickets(Player p) throws SQLException {
    rTickets.clear();
    int t = 0;
    Statement statement = MySQL.getConnection().createStatement();
    ResultSet r2 = statement
            .executeQuery("SELECT COUNT(*) FROM tickets ORDER BY Date DESC;");
    while (r2.next()) {
        t = r2.getInt(1);
    }
    if (t > 5) {
        ResultSet result = statement.executeQuery("SELECT * FROM tickets ORDER BY Date DESC LIMIT 5;");
        result.next();

        int i;
        for (i = 0; i < t; i++) {
            rTickets.put(result.getInt("TicketID"), result.getString("UUID"));
            result.next();
        }
        return rTickets;
    } else {
        ResultSet result = statement.executeQuery(
                "SELECT * FROM tickets ORDER BY Date DESC LIMIT " + t + ";");
        result.next();

        int i;
        for (i = 0; i < t; i++) {
            rTickets.put(result.getInt("TicketID"), result.getString("UUID"));
            result.next();
        }

        return rTickets;
    }

}
}

2 个答案:

答案 0 :(得分:2)

问题在于result.next();.在for循环中调用result.next()作为第一个语句,并在for循环之前删除方法调用。你可以重构代码如下。

public static HashMap<Integer, String> recentTickets(Player p) throws SQLException {
    rTickets.clear();
    int t = 0;
    Statement statement = MySQL.getConnection().createStatement();
    ResultSet r2 = statement
        .executeQuery("SELECT COUNT(*) FROM tickets ORDER BY Date DESC;");
    while (r2.next()) {
      t = r2.getInt(1);
    }
    if(t>5){
        t=5;
    }

    ResultSet result = statement.executeQuery(
            "SELECT * FROM tickets ORDER BY Date DESC LIMIT " + t + ";");

        for (int i = 0; i < t; i++) {
            result.next();
            rTickets.put(result.getInt("TicketID"), result.getString("UUID"));

        }
        return rTickets;        
}

答案 1 :(得分:1)

看起来你正在调用result.next()比你想要的还要多一点。

将while.next()放入while循环中总是更好。

public static HashMap<Integer, String> recentTickets(Player p) throws SQLException {
    rTickets.clear();
    int t = 0;
    Statement statement = MySQL.getConnection().createStatement();
    ResultSet r2 = statement
            .executeQuery("SELECT COUNT(*) FROM tickets ORDER BY Date DESC;");
    while (r2.next()) {
        t = r2.getInt(1);
    }
    ResultSet result;
    if (t > 5) {
       result = statement.executeQuery("SELECT * FROM tickets ORDER BY Date DESC LIMIT 5;");

    } else {
        result = statement.executeQuery(
                "SELECT * FROM tickets ORDER BY Date DESC LIMIT " + t + ";");
    }
    if(result == null){
     return rTickets;
    }
    while(result.next()){
           rTickets.put(result.getInt("TicketID"), result.getString("UUID"));
    }
        return rTickets;
}
}