ResultSet关闭后不允许操作(mysql,java) [英] Operation not allowed after ResultSet closed (mysql, java)

查看:128
本文介绍了ResultSet关闭后不允许操作(mysql,java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误,这里我的第42行是而(rs.next()){,请帮我解决这个问题我被困在这里几个小时。

I was stuck with the error , here my line number 42 is while(rs.next()){, please help me with this i am stuck at this for few hrs.

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

}


推荐答案

Statement 对象只能有一个活动的 ResultSet ,所以当你执行 rs2时= stmt.executeQuery(sql),第一个ResultSet( rs )被关闭。

A Statement object can have only one active ResultSet, so when you execute rs2 = stmt.executeQuery(sql), the first ResultSet (rs) gets closed.

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

Create two Statement objects, one for rs and another for rs2.

引用 Statement


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

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

这篇关于ResultSet关闭后不允许操作(mysql,java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆