0

在阅读了很多类似的问题后,我无法找到适合我的解决方案。

我有这个方法:

在 crawler4j 控制器中,我这样做:

        ArrayList<String> urls = Urls.getURLs(100);

    for (String s : urls) {
        System.out.println("Adding URL: " + s);
        controller.addSeed(s);

    }

这是 getURLs():

    public static ArrayList<String> getURLs(int number) {

    ArrayList<String> list = new ArrayList<String>();


    String getStatement = "select * from " + Configurations.getStringProperty("mysql.urls.db_name", "urls") + " where retrieved=0 limit "
            + Configurations.getStringProperty("mysql.urls.limit", "100") + ";";

    ResultSet rs;
    rs = Databaseclient.executeStatement(getStatement);




    try {
        while (rs.next()) {
            list.add(rs.getString("url"));
            // Databaseclient.executeStatement("update urls set retrieved = true where id = "
            // + rs.getInt("id") + ";");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return list;
}

这是我的执行语句():

public static ResultSet executeStatement(String s) {

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    try {
        // fetch a connection
        connection = DataSource.getInstance().getConnection();

        if (connection != null) {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(s);
        }

    } catch (SQLException e) {
        System.out.println("A SQLException occured executing the Statement");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("A IOException occured executing the Statement");
        e.printStackTrace();
    } catch (PropertyVetoException e) {
        System.out.println("A PropertyVetoException occured executing the Statement");
        e.printStackTrace();
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                System.out.println("A SQLException occured executing the Statement");

                e.printStackTrace();
            }

        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                System.out.println("A SQLException occured executing the Statement");
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                System.out.println("A SQLException occured executing the Statement");
                e.printStackTrace();
            }
        }

    }
    return resultSet;

}

我得到错误

java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:799)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6982)
at database.Urls.getURLs(Urls.java:27)
at crawler.Controller.main(Controller.java:53)

哪条线

            while (rs.next()) {

在我的 getURLs() 方法中。

我究竟做错了什么?在获取语句和可以关闭语句的 while 循环之间没有代码。

4

1 回答 1

1

您的代码有点偏离,但如果我理解您,请不要ResultSet在您的方法的 finally 块中关闭您的代码executeStatement

public static ResultSet executeStatement(Connection connection, 
    Statement statement, String s) {
  ResultSet resultSet = null;
  try {
    if (statement != null) {
      resultSet = statement.executeQuery(s);
    }
  } catch (SQLException e) {
    System.out.println("A SQLException occured executing the Statement");
    e.printStackTrace();
  } catch (IOException e) {
    System.out.println("A IOException occured executing the Statement");
    e.printStackTrace();
  } catch (PropertyVetoException e) {
    System.out.println("A PropertyVetoException occured executing the Statement");
    e.printStackTrace();
  }
  return resultSet;
}

然后你需要传入一个 Connection 和 Statement,你会得到一个 ResultSet 回来。此外,调用者应在使用 ResultSet 完成后关闭所有三个。

于 2014-07-11T13:29:35.463 回答