尝试执行查询时,会抛出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();
}
}
答案 0 :(得分:1)
您正在通过调用
关闭ResultSetresultSet.close();
之后,您将返回resultSet并调用resultSet.next()
方法。关闭next
后,您不应该调用ResultSet
方法。
要解决此问题,请在获取数据后关闭ResultSet
。