Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
结果集关闭后不允许操作(mysql、java)_Java_Mysql_Sql - Fatal编程技术网

结果集关闭后不允许操作(mysql、java)

结果集关闭后不允许操作(mysql、java),java,mysql,sql,Java,Mysql,Sql,我被错误卡住了,这里我的行号42是,而(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.SQLErr

我被错误卡住了,这里我的行号42是
,而(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();
    }

}

一个
语句
对象只能有一个活动的
结果集
,因此当执行
rs2=stmt.executeQuery(sql)
时,第一个结果集(
rs
)将关闭

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

引用以下文件的javadoc:

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


一个
语句
对象只能有一个活动的
结果集
,因此当执行
rs2=stmt.executeQuery(sql)
时,第一个结果集(
rs
)将关闭

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

引用以下文件的javadoc:

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


一条语句的一个结果集是有效的。当您执行多个查询时,请使用不同的语句

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+'"';

            Statement stmt2 = conn.createStatement();
            rs2 = stmt2.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 + "";

            Statement stmt3 = conn.createStatement();
            rs3 = stmt3.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();
}

一条语句的一个结果集是有效的。当您执行多个查询时,请使用不同的语句

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+'"';

            Statement stmt2 = conn.createStatement();
            rs2 = stmt2.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 + "";

            Statement stmt3 = conn.createStatement();
            rs3 = stmt3.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();
}

JDBC不允许您关闭创建ResultSet的语句,也不允许您执行使用同一语句创建ResultSet的另一个查询。如果要创建不同的语句对象和ResultSet对象,请确保不要使用相同的语句对象来执行两个不同的ResultSet语句。

JDBC不允许您关闭ResultSet语句创建ResultSet的语句,或执行使用同一语句创建ResultSet的另一个查询。如果要创建不同的语句对象和ResultSet对象,请确保不要使用相同的语句对象来执行两个不同的ResultSet语句

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+'"';

            Statement stmt2 = conn.createStatement();
            rs2 = stmt2.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 + "";

            Statement stmt3 = conn.createStatement();
            rs3 = stmt3.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();
}