Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在Java中执行查询时引发异常_Java_Mysql - Fatal编程技术网

尝试在Java中执行查询时引发异常

尝试在Java中执行查询时引发异常,java,mysql,Java,Mysql,当尝试执行查询时,它抛出一个SQLException(ResultSet关闭后不允许执行该操作)。 不过,已建立连接 代码: public ResultSet executeQuery(String sql, Object... parameters) { try (Connection c = dataSource.getConnection(); PreparedStatement stmt = c.prepareStatement(sql, Statement.RETURN

当尝试执行查询时,它抛出一个SQLException(ResultSet关闭后不允许执行该操作)。 不过,已建立连接

代码:

public ResultSet executeQuery(String sql, Object... parameters) {
        try (Connection c = dataSource.getConnection(); PreparedStatement stmt = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            for (int i = 0; i < parameters.length; i++) {
                stmt.setObject(i + 1, parameters[i]);
            }

            ResultSet resultSet = stmt.executeQuery();

            resultSet.close();
            stmt.close();
//            c.close();

            return resultSet;
        } catch (SQLException e) {
            throw new IllegalStateException(e);
        }
    }
java.sql.SQLException: Operation not allowed after ResultSet closed
        at com.hylexismc.profiles.Main.getProfile(Main.java:127) ~[?:?]
        at com.hylexismc.profiles.Main.hasProfile(Main.java:107) ~[?:?]
        at com.hylexismc.profiles.Main.lambda$onEnable$0(Main.java:50) ~[?:?]
        at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at org.bukkit.craftbukkit.v1_11_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:738) [spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
Caused by: java.sql.SQLException: Operation not allowed after ResultSet closed
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6301) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-2b6c9f4]
        at com.hylexismc.profiles.Main.getProfile(Main.java:115) ~[?:?]
        ... 9 more
编辑1: 现在,我改变了它,它给出了完全相同的结果,这是如何工作的

public ResultSet executeQuery(String sql, Object... parameters) {
            Connection c = null;
            PreparedStatement stmt = null;
            ResultSet resultSet = null;

            try {
                c = dataSource.getConnection();
                stmt = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

                for (int i = 0; i < parameters.length; i++) {
                    stmt.setObject(i + 1, parameters[i]);
                }

                resultSet = stmt.executeQuery();
                return resultSet;
            } catch (SQLException e) {
                throw new IllegalStateException(e);
            } finally {
                try {
                    assert c != null;
                    assert stmt != null;
                    assert resultSet != null;

                    c.close();
                    stmt.close();
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
public ResultSet executeQuery(字符串sql、对象…参数){
连接c=null;
PreparedStatement stmt=null;
ResultSet ResultSet=null;
试一试{
c=dataSource.getConnection();
stmt=c.prepareStatement(sql,Statement.RETURN\u生成的\u键);
对于(int i=0;i
您正在通过调用

resultSet.close();
然后返回结果集并调用
resultSet.next()
方法。关闭
resultSet
后,不应调用
next
方法


要解决此问题,请在获取数据后关闭
ResultSet

在调用
ResultSet.close()
(以及在调用
stmt.close()
)之前,需要先读取结果;您应该在
最后
块中同时关闭这两个。一旦您关闭其中任何一个,您将无法检索更多值。我更改了它,但出现了相同的异常,请查看编辑。好的,这确实有效。但我不需要关闭连接吗?我听说这是最佳做法。您应该关闭
结果集
。您需要吗可以使您的方法返回所需的数据,而不是
结果集