java - 在 Java 和 PostgreSQL 中使用 createArrayOf 时,ResultSet 为空
问题描述
我正在尝试使用 s 数组查询产品id
表。这是该方法的一个片段:
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
s 为 1、2 和 3。products
返回 null。知道为什么吗?
谢谢
编辑
右侧是标量表达式的括号列表
例子(1,2,3)
所以,我认为问题变成了:如何从 my 获取标量表达式列表ArrayList
?
解决方案
要在查询的 WHERE 子句中检查元素是否在数组中,您可以使用 ANY。在你的情况下:
SELECT * FROM product WHERE id = ANY(?)
推荐阅读
- flask - 在整个应用程序中扩展 context_processor(包括蓝图)
- javascript - HTML单选按钮在数据库中保存第一个单选按钮值
- java - 无法将 springboot REST api 部署到 Heroku
- neo4j - 在 Neo4J 中查找图中节点之间的长度(最远)
- arrays - 谷歌表格 IF 函数返回值
- c++ - 如何在 omnet++ 中将 simtime 转换为双值?
- java - Spring Boot Junit 使用构造函数注入测试注入 dao 接口
- javascript - Elasticsearch:跨多个索引返回唯一记录
- java - Java Regex 匹配 Pattern 或 Pattern 中的任何子字符串
- primefaces - 无法链接 org/primefaces/application/resource/DynamicResourcesPhaseListener:javax/faces/event/PhaseListener