0

I have developed a method that returns the resultset of the query I pass

method:

public ResultSet executeQuery(String query)
{
    ResultSet rs = null;

    try
    {
        stmt = _conn.createStatement();
        rs = stmt.executeQuery(query);
    }
    catch(Exception e)
    {
        System.out.println("errore: "+e);
    }
    finally
    {
        try
        {
            //chiusura connessione
            stmt.close();
            _conn.close();
        }
        catch (Exception e) 
        {
            System.out.println("errore: "+ e);
        }
    }
    return rs;
}

I recalled it in my web service but it gives me an error

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

    String sql="select .........";
    Database db1 = new Database();
    ResultSet rs = db1.executeQuery(sql);


    try
    {
        while(rs.next())
        {
            code = rs.getString("codcon"); 
            list.add(code);
        }
    }

the error gives me when it enters the while loop

java.sql.SQLException: Operation not allowed after ResultSet closed
mego
  • 3
  • 3
  • If you close the statement and the connection, there is no chance you can retrieve anything from the resultset afterwards. Consider populating your list in the method that executes the query, then return the list, not the resultset. – Arnaud Jan 23 '19 at 08:07
  • @Arnaud so to close the connection I should do .close of the resultset in my web services? – mego Jan 23 '19 at 08:09
  • No, just populate your list inside your `executeQuery` method , and return it instead of returning the resulset. – Arnaud Jan 23 '19 at 08:11

1 Answers1

0

ResultSet doesn't contains data directly. It query server for them.

When you close connection ResultSet can't access server anymore.

talex
  • 17,973
  • 3
  • 29
  • 66