在PostgreSQL与Java一起使用createArrayOf时ResultSet为空

问题描述

我正在尝试使用id s数组查询 product 表。这是该方法的一部分:

PreparedStatement statement = connection
    .prepareStatement("SELECT * FROM product WHERE id IN (?)");

System.out.println(ids /*ArrayList<Integer>*/); //prints [3]

Array array = connection.createArrayOf("INTEGER",ids.toArray());
// Array array = connection.createArrayOf("INTEGER",new Integer[]{1,2,3}); //<-----tried this too

statement.setArray(1,array);

ResultSet results = statement.executeQuery();

while (results.next()) {
    System.out.println("does not print this");
    Product product = new Product(0);
    product.setId(results.getInt("id"));
    products.add(product);
}

return products;

product包含3行,其中id分别为1、2和3。products返回null。知道为什么吗?

谢谢

编辑

根据section 9.23.1

右侧是标量表达式的括号列表

示例(1,3)

因此,我认为问题变成了:如何从我的ArrayList获取标量表达式列表?

解决方法

要检查查询的WHERE子句,元素是否在数组中,可以使用ANY。就您而言:

SELECT * FROM product WHERE id = ANY(?)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...