java.sql.SQLException:ResultSet关闭MySQL Java后不允许操作

每当我尝试使用这种方法时,我都会继续这样做.

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();
}

这是请求的查询方法.

/**
 * 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对象来更新数据(在查询方法中),从而从查询中关闭ResultSet.

ResultSet documentation

“A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.”

您应该为查询和更新创建单独的语句,并将它们传递给您的查询方法:

public ResultSet query(String s, Statement statement) throws SQLException {
    ...
}

我假设该语句在类中声明为static – 通常不需要:在类的构造函数中创建用于查询和更新的两个语句,并将它们传递给query()方法或使用一个或者其他取决于声明.

上一篇:java – “if(rs.next())”是什么意思?


下一篇:Unity Timeline自定义轨道 DefaultPlayables源码剖析