0

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")?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
arkani
  • 7
  • 7

1 Answers1

1

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

Community
  • 1
  • 1
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • That is right. But while(rs.next()) { System.out.println(rs.getString("columnA")); } did not print anything.. How can I fix it? Thank you so much – arkani Apr 07 '16 at 10:31
  • By making a query that doesn't return an empty resultset. – Kayaman Apr 07 '16 at 10:31
  • Try using a `Statement` and concatenating the ids to the query to see if it works (with the `'` delimiters for the ids if they're `TEXT`). It may be that the SQLite driver doesn't like the way you're using that prepared statement. – Kayaman Apr 07 '16 at 10:34
  • Could you help me to write it right? you mean it? //String question = "(?)"; PreparedStatement pst=connection.prepareStatement("select`"+columnA+"` from '"+TableUser +" where ' = '"+id+"' = '"+question+"'"); – arkani Apr 07 '16 at 10:48
  • See the link for discussion about how to work with the `IN` clause and `PreparedStatement` (it's still a lot better than using a raw `Statement`). – Kayaman Apr 07 '16 at 10:51