【问题标题】:java.sql.SQLException: ResultSet closed in SQLitejava.sql.SQLException: ResultSet 在 SQLite 中关闭
【发布时间】:2019-09-05 17:58:10
【问题描述】:

我正在使用 SQLite。当我试图获得一个 int 时,它不起作用。 我在这个程序的早期使用了这个 exaclty,但现在它不再起作用了。我希望,你能帮助我。 PS:Sry 4 我的英语不好。 我打算在这里写一些随机的想法,因为我这个窗口说,我必须多写几句话,因为代码比文本多。

获取方法

    public static boolean getBold(UUID uuid) {
        try {
            PreparedStatement state = MySQL.c.prepareStatement("SELECT * From Chat where uuid=?");
            state.setString(1, uuid.toString());

            ResultSet rs = state.executeQuery();
            rs.next();

            int i = rs.getInt("bold");


            rs.close();
            state.close();

            if(i == 1){
                return true;
            }else {
                return false;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return false;

    }

错误:

[19:42:36] [Server thread/WARN]: java.sql.SQLException: ResultSet closed
[19:42:36] [Server thread/WARN]:    at org.sqlite.RS.checkOpen(RS.java:63)
[19:42:36] [Server thread/WARN]:    at org.sqlite.RS.findColumn(RS.java:108)
[19:42:36] [Server thread/WARN]:    at org.sqlite.RS.getInt(RS.java:293)
[19:42:36] [Server thread/WARN]:    at de.primeapi.chatsystem.mysql.ChatStats.getBold(ChatStats.java:57)
[19:42:36] [Server thread/WARN]:    at de.primeapi.chatsystem.commands.BoldCommand.onCommand(BoldCommand.java:22)
[19:42:36] [Server thread/WARN]:    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
[19:42:36] [Server thread/WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
[19:42:36] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13)
[19:42:36] [Server thread/WARN]:    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[19:42:36] [Server thread/WARN]:    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
[19:42:36] [Server thread/WARN]:    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
[19:42:36] [Server thread/WARN]:    at java.lang.Thread.run(Thread.java:748)

我的 SQL 类:

    public static Connection c;

    public static void connect() {
        try {
            Class.forName("org.sqlite.JDBC");
            String url = "jdbc:sqlite:plugins/ChatSystem/database.sql";
            c = DriverManager.getConnection(url);
            System.out.println("[SQLite] Connected.");
        } catch (SQLException e) {
            System.out.println("[SQLite] Connection failed.");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void disconnect() {
        if (c != null) {
            try {
                c.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void update(String qry) {
        try {
            Statement stmt = c.createStatement();
            stmt.executeUpdate(qry);
            stmt.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
            disconnect();
            connect();
        }
    }

【问题讨论】:

  • 我敢打赌你的查询是空的,即SELECT 没有返回任何内容,即rs.next() 返回false,而你没有检查。
  • 仅供参考:您应该使用 try-with-resources。

标签: java sql sqlite


【解决方案1】:

当您重复使用 SQL 语句或查询为空时,会出现错误 java.sql.SQLException: ResultSet closed。你可以开始检查这两点。

【讨论】:

    猜你喜欢
    • 2017-04-16
    • 2011-06-19
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多