问题描述
我正在尝试使用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。知道为什么吗?
谢谢
编辑
右侧是标量表达式的括号列表
示例(1,3)
因此,我认为问题变成了:如何从我的ArrayList
获取标量表达式列表?
解决方法
要检查查询的WHERE子句,元素是否在数组中,可以使用ANY。就您而言:
SELECT * FROM product WHERE id = ANY(?)