Creating java.sql.Array as an array of integers for use in prepared statement

Viewed 2771

I have a need to use a prepared statement to use an array of integer in my SQL statement.

When I try to create the array, and when I read the Javadoc it says It only shows the creation of an array of strings. I have to search for a lot of table key ids in one call (previously iterating through them one at time resulted in 22k queries) So I need all of the results at one time.

A piece of my query string looks like this

AND A.KEY in (?) order by KEYID DESC

Here is where I build my query (via using java.sql.Array with my list of IDs)

StringBuilder query = new StringBuilder(Query);             

PreparedStatement stmt = connection.prepareStatement(query.toString());

java.sql.Array array = connection.createArrayOf("varchar", Ids.toArray());
stmt.setArray( 1, array);

ResultSet results = stmt.executeQuery();

I tried to run them through as strings, as the sql.Array uses strings, but I NEED to send them as ints.

My ids come through as an ArrayList<Integer>, this is the key to why this is not a duplicate question I need to send the sql statement a list of integers - my way works for strings.

edit when i run through my program sending one int at a time, it works fine.

How can I solve this to send the SQL statement with integers in place of my? In the sql query?

1 Answers
Related