3

How do I set an array of integers in JDBC (prepared statement with setArray for an in clause?

String query = "SELECT * FROM table WHERE id IN (?)";
// other things.
// ArrayList<Integer> some_ids;
preparedStatement.setArray(1, conn.createArrayOf("INTEGER", some_ids.toArray()));

It compiles, but executing returns this from Postgres.

ERROR: cannot cast type integer[] to integer

1 Answer 1

5

In PostgreSQL You have to use = ANY(?) instead of IN (?)

"SELECT * FROM table WHERE id = ANY(?)";

Take a look at this : 9.21.3. ANY/SOME (array)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.