简体   繁体   中英

How to use Array or List<> in one PrepareStatement (sqlite)

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM