问题描述
无论何时尝试使用此方法,我都会不断得到它.
I keep getting this whenever I try to use this method.
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7077)
at server.util.Plimus$1.run(Plimus.java:77)
这是第77行:while (resultSet.next()) {
public static void process(final Player c, final String playerName) {
if (connection == null && connectionStatus == 0)
createConnection();
else if (connectionStatus != 0)
return;
new Thread() {
@Override
public void run() {
try {
String username = playerName.replaceAll(" ", "_");
String query = "SELECT * FROM donations WHERE username = '" + username + "' AND received = '0' LIMIT 1;";
ResultSet resultSet = query(query);
while (resultSet.next()) {
int[] contractIds = {3178768, 1}; //put all of your contract ids in here.
int contractId = Integer.parseInt(resultSet.getString("contract")), id = Integer.parseInt(resultSet.getString("id"));
query("UPDATE donations SET received = '1' WHERE username = '" + username + "' AND id = '" + id + "';");
if (contractId == contractIds[0]) { //first contract id in array.
c.getItems().addItem(962, 1);
} else if (contractId == contractIds[1]) { //second contract id in array.
c.getItems().addItem(962, 1);
}
}
} catch (Exception e) {
e.printStackTrace();
processMethod(0, true, true, false);
}
}
}.start();
}
这是所请求的查询方法.
Here is the query method that is requested.
/**
* Creates query function.
*/
public static ResultSet query(String s) throws SQLException {
try {
if (s.toLowerCase().startsWith("select")) {
ResultSet resultSet = statement.executeQuery(s);
return resultSet;
} else {
statement.executeUpdate(s);
}
return null;
} catch (Exception e) {
e.printStackTrace();
processMethod(0, true, true, false);
}
return null;
}
推荐答案
在遍历ResultSet
时,您正在重用Statement
对象来更新数据(在query
方法内部),这将关闭ResultSet
来自查询.
While iterating through your ResultSet
, you are reusing the Statement
object for updating the data (inside your query
method), which closes the ResultSet
from the query.
请参见 ResultSet文档:
您应该为查询和更新创建单独的语句,然后将它们传递给您的查询方法:
You should create separate Statements for the query and the update, and pass them to your query method:
public ResultSet query(String s, Statement statement) throws SQLException {
...
}
我假设statement
在类中声明为static
-通常不需要这样做:在类的构造函数中创建两个要查询和更新的语句,然后将它们传递给query()
方法或使用一个或另一个取决于语句.
I assume that statement
is declared static
inside your class - there is usually no need for that: create the two statements for query and update in the constructor of your class, and either pass them to the query()
method or use the one or the other depending on the statement.
这篇关于java.sql.SQLException:ResultSet关闭MySQL Java后不允许进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!