Dan*_*ner 2 postgresql jdbc vert.x
我有一个需要查询运行 Postgres 10.7 的 AWS RDS 实例的 Vert.x Web 应用程序。Vert.x JDBC 客户端是io.vertx:vertx-jdbc-client:3.8.4. 我想查询一个表,约束条件是某个列的值包含在一组值中:
select from table where column in/any (?)
我遵循了 Vertx 文档,它说创建一个JsonArray并用值填充它以注入查询。该列属于类型text,我想要匹配的列表是 Java ArrayList<String>。我的查询代码如下所示:
String sql = "SELECT a FROM table WHERE col IN (?)";
List<String> values = someObject.someField();
sqlClient.getConnection(connectionResult -> {
if (connectionResult.failed()) {
// handle
} else {
SQLConnection connection = connectionResult.result();
JsonArray params = new JsonArray()
.add(values);
connection.queryWithParams(sql, params, queryResult -> {
if (queryResult.failed()) {
// handle
} else {
// parse
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
查询失败并显示错误: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of io.vertx.core.json.JsonArray. Use setObject() with an explicit Types value to specify the type to use.
我知道在最坏的情况下,我可以创建一个文字 SQL 字符串where col in (?, ?, ?, ..., ?)并将列表中的每个字符串添加到 a JsonArray,但必须有一种方法可以将 仅ArrayList<String>作为参数添加并保持查询简单。如何指定要在查询中匹配的值列表?
Vert.x JDBC Client 不支持查询中的数组参数。
然而,Vert.x Pg Client是可能的,它不依赖于 JDBC。您需要先修改您的查询:
SELECT a FROM table WHERE col = ANY(?)
Run Code Online (Sandbox Code Playgroud)
然后:
pgClient.preparedQuery(query, Tuple.of(possibleValues), collector, handler);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
697 次 |
| 最近记录: |