java.sql.SQLException: Operation not allowed after ResultSet closed 6

112 Views Asked by At

First of all I'm new to this java. Here is my code:

public class functions {

private static Connection connection;
private static ArrayList<MapModel> list = new ArrayList<MapModel>();
private static Cipher cipher;

public static ArrayList<MapModel> getAllId(String report_type, String dist_code){
    ArrayList<MapModel> idList = new ArrayList<MapModel>();
    ResultSet rs = null;
    Statement statement = null;
    try {

        connection = DBconnection.getConnection();
        System.out.println(connection.getMetaData());
        statement = connection.createStatement();
        String sproc=null;
        sproc="call load_district('" + report_type + "','" + dist_code + "')";
        System.out.println("call load_district('" + report_type + "','" + dist_code + "')");
        CallableStatement cstmt = connection.prepareCall("{"+sproc+"}");
        rs = cstmt.executeQuery();

        System.out.println(rs.isClosed());
        System.out.println(statement.isClosed());
        System.out.println(connection.isClosed());
        if (!rs.isBeforeFirst()) {
            System.out.println("no data");
        } else {
            while (rs.next()) {
                //some data
            }
        }

    } catch (Exception e) {

        e.printStackTrace();

    } finally {
        try {
            // just to check
            System.out.println("m closing all");
            System.out.println(rs.isClosed());
            System.out.println(statement.isClosed());
            System.out.println(connection.isClosed());
            // ---end---
            DbUtils.closeQuietly(statement);
            DbUtils.closeQuietly(rs);
            DbUtils.closeQuietly(connection);
        } catch (SQLException ex) {
            Logger.getLogger(functions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return idList;

}

}

Not always, but sometime I get this error:

Info:   call load_district('someval','someval')
Info:   false
Info:   true
Info:   true
Info:   m closing all
Info:   true
Info:   true
Info:   true
Severe:   java.sql.SQLException: Operation not allowed after ResultSet closed

I don't understand why the statement and connection are in a closed state before they are closed.

0

There are 0 best solutions below