PostgreSQL & JDBC 使用 `setArray` 设置整数数组抛出异常 "cannot cast type integer[] to integer"

PostgreSQL & JDBC set array of ints using `setArray` throw an exception "cannot cast type integer[] to integer"

如何在 JDBC 中设置一个整数数组(为 in 子句准备好的带有 setArray 的语句?

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

它编译,但从 Postgres 执行returns。

ERROR: cannot cast type integer[] to integer

在 PostgreSQL 中你必须使用 = ANY(?) 而不是 IN (?)

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

看看这个:9.21.3. ANY/SOME (array)