0

After reading through a lot of similar questions I have not been able to get a solution that works for me.

I have this methods:

In a crawler4j Controller I do this:

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

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

    }

this is 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;
}

This is my executeStatement():

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;

}

I get the error

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)

Which is the line

            while (rs.next()) {

in my getURLs() method.

What am I doing wrong? There is no code between getting the statement and the while loop that could close the statement.

pinpox
  • 179
  • 2
  • 10

1 Answers1

1

Your code is a bit off, but if I understand you then don't close your ResultSet in the finally block of your executeStatement method.

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

Then you need to pass in a Connection and Statement, and you'll get a ResultSet back. Also, the caller should then close all three when it's done with the ResultSet.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I removed the finally block and it works! Thanks! I'll change the arguments of executeStatement() now. – pinpox Jul 11 '14 at 13:40