Search code examples
javasqlitearraylistresultset

Error "ResultSet closed"


Why do I get this error :

Error :java.sql.SQLException: ResultSet closed

from this code:

try{   
        ArrayList<String> longArray = new ArrayList<String>();
        longArray.add("12345678912"); // All column in sqlite database are "text" so that's why I create String List 
        longArray.add("12345678911"); 
        System.out.println(longArray);
        String parameters = StringUtils.join(longArray.iterator(),",");  
        connection = sqliteConnection.dbConnector();  
        PreparedStatement pst=connection.prepareStatement("select columnA from TableUser where id in (?)");    
        pst.setString(1, parameters ); 
        ResultSet rs = pst.executeQuery(); 

        System.out.println(rs.getString("columnA")); // did not print anything, probably rs is empty
        rs.close();

Database details:
I have a table TableUser in database where there is a column "id" as Text.

Another question : Is value in database 12345678912 (TEXT) the same as longArray.add("12345678912")?


Solution

  • You're missing an if(rs.next()) or while(rs.next()) after you retrieve the ResultSet.

    For your other question...yes, they're both Strings aren't they?

    Edit: The problem was essentially trying to put multiple parameters into the IN statement in a PreparedStatement. See PreparedStatement IN Clause Alternatives