小编典典

关闭ResultSet后不允许进行操作(mysql,java)

sql

我被错误卡住了,这里的第42行是while(rs.next()){,请帮我解决这个问题,我在这里待了几个小时。

> Exception in thread "main" java.sql.SQLException: Operation not allowed after ResultSet closed
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
    at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:740)
    at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6326)
    at removeStopwords.RemoveStopwords.main(RemoveStopwords.java:42)

这是我的代码:

package removeStopwords;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.StringTokenizer;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class RemoveStopwords {

    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/mydbv2";

    // Database credentials
    static final String USER = "root";
    static final String PASS = "***";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Connection conn = null;
        Statement stmt = null;

        Class.forName("com.mysql.jdbc.Driver");

        conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = (Statement) conn.createStatement();
        String sql;
        ResultSet rs = null;
        ResultSet rs2 = null;
        ResultSet rs3 = null;
        java.sql.PreparedStatement ps = null;
        int event_id = 10;
        sql = "SELECT id,text from tweet where event_id = " + event_id;
        rs = stmt.executeQuery(sql);

        String text = "";
        Long id;
        while (rs.next()) {
            id = rs.getLong("id");
            text = rs.getString("text");
            System.out.println("tweet = " + text);
            text = text.replaceAll("http[^\\s]+", "");
            text = text.replaceAll("www[^\\s]+", "");
            System.out.println("tweet after removal of links= " + text);

            StringTokenizer st = new StringTokenizer(text);
            while (st.hasMoreTokens()) {
                String stopword = st.nextToken();
                System.out.println("stopword : " + stopword);

                sql = "SELECT * from stopwords WHERE word =" + '"'+stopword+'"';
                rs2 = stmt.executeQuery(sql);
                if (rs2.next()) {
                    text = text.replaceAll(stopword, "");
                    System.out.println("tweet after removing stopword = " + text);
                }
                sql = "SELECT * from filtertweet where tweet_id = " + id + "";
                rs3 = stmt.executeQuery(sql);
                if (!rs3.next()) {
                    sql = "INSERT INTO filtertweet VALUES(?,?)";
                    ps = conn.prepareStatement(sql);
                    ps.setLong(1, id);
                    ps.setString(2, text);
                    ps.executeUpdate();
                }

            }
        }
        stmt.close();
        conn.close();
    }

}

阅读 263

收藏
2021-03-10

共1个答案

小编典典

一个Statement对象只能具有一个active对象ResultSet,因此在执行时rs2 = stmt.executeQuery(sql),第一个ResultSet(rs)被关闭。

创建两个Statement对象,一个用于rs,另一个用于rs2

引用以下内容的javadoc
Statement

默认情况下,ResultSet每个Statement对象只能同时打开一个对象。因此,如果一个ResultSet对象的读取与另一个对象的读取是交错的,则每个Statement对象必须已由不同的对象生成。如果存在打开Statement的语句,则该接口中的所有执行方法都会隐式关闭该语句的当前ResultSet对象。

2021-03-10