0

I try a lot of time to solve this so thank you for any help. How to use array in preparestatement? I do it like in this example How to use an arraylist as a prepared statement parameter but it return me error, my code:

        //initialize
        ResultSet rs2; 
        connection2 = sqliteConnection.dbConnector(); 
        PreparedStatement statement2=connection2.prepareStatement("SELECT surname FROM users where id IN (?)");

        //like in the example linked, but it did not work:   
        Array array = connection2.createArrayOf("VARCHAR", new Object[]{"1", "2"});
        statement2.setArray(1, array);
        rs2=statement2.executeQuery();

        //to keep data from resultset, it works fine
        List<String> rsDATA = new ArrayList<String>();
        while(rs2.next()){ 
            rsDATA.add(rs2.getString("surname")); 
        }   
        for(int i = 0; i < rsDATA.size(); i++) {
            System.out.println(rsDATA.get(i));
        }
        connection.close();

but it return error:

Exception in thread "AWT-EventQueue-0" java.lang.AbstractMethodError: org.sqlite.SQLiteConnection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array;
at lines: 

1)Array array = connection2.createArrayOf("VARCHAR", new Object[]{"1", "2"});
2(executeQuery();

It is very important for me to solve this. Thank you a lot..

Community
  • 1
  • 1
arkani
  • 7
  • 7

1 Answers1

0

You need to use IN in your WHERE clause:

SELECT surname FROM users WHERE id IN (?)

and a java.sql.Array object as shown in the answer to the linked question.

wero
  • 32,544
  • 3
  • 59
  • 84
  • I do it like you hint me, but as you can see it still return error, how it can be change? What I can do with it? – arkani Apr 07 '16 at 08:10
  • 1
    @arkani your sqlite JDBC driver is probably outdated, see http://stackoverflow.com/questions/16408591/java-lang-abstractmethoderror-org-sqlite-conn-createarrayof-error-while-calling. But as indicated in that answer, sqlite may not have a `java.sql.Array` implementation. – wero Apr 07 '16 at 08:12
  • 1
    It true, sqlite not have java.sql.Array implementation. So is there any another way to do it? It is a big project and I can't change jdbc.. Can statement can be done with some loop to skip array problem? – arkani Apr 07 '16 at 08:32