如何使用 jdbc 驱动程序在 java 中正确执行 `SET` 查询
how to properly execute a `SET` query in java using jdbc driver
我可以连接到我的数据库,但无法正确执行 SET
查询。通过查看文档,我应该使用 execute
而不是 executeQuery
或 executeUpdate
。我有一个工作 connection
,这是其余的 :
try (Statement st = connection.createStatement()) {
boolean result = st.execute("SET search_path TO '712275-8S8DH-74DASS'");
ResultSet queryResult = st.executeQuery("SELECT MAX(customer.last_updated) from customer");
while (queryResult.next()) {
String lastUpdated = queryResult.getString("last_updated");
}
} catch (SQLException ex) {
LOGGER.info(ex.getMessage());
}
我得到这些异常:The column name last_updated was not found in this ResultSet.
和 No data from query
这让我觉得 search_path 没有正确设置,因为当我使用 psql
建立直接连接时, 我可以通过 运行
得到结果
postgres=> SET search_path TO '712275-8S8DH-74DASS';
SET
postgres=> SELECT MAX(customer.last_updated) from customer;
[expected result]
您的查询没有 return 名为 last_updated
的列中的结果。
那是因为你正在进行聚合操作。
我认为查询编辑的 return 列称为 max
.
这可能有效:
String lastUpdated = queryResult.getString("max");
或者您可以使用别名:
ResultSet queryResult = st.executeQuery("SELECT MAX(customer.last_updated) as last_updated from customer");
我可以连接到我的数据库,但无法正确执行 SET
查询。通过查看文档,我应该使用 execute
而不是 executeQuery
或 executeUpdate
。我有一个工作 connection
,这是其余的 :
try (Statement st = connection.createStatement()) {
boolean result = st.execute("SET search_path TO '712275-8S8DH-74DASS'");
ResultSet queryResult = st.executeQuery("SELECT MAX(customer.last_updated) from customer");
while (queryResult.next()) {
String lastUpdated = queryResult.getString("last_updated");
}
} catch (SQLException ex) {
LOGGER.info(ex.getMessage());
}
我得到这些异常:The column name last_updated was not found in this ResultSet.
和 No data from query
这让我觉得 search_path 没有正确设置,因为当我使用 psql
建立直接连接时, 我可以通过 运行
postgres=> SET search_path TO '712275-8S8DH-74DASS';
SET
postgres=> SELECT MAX(customer.last_updated) from customer;
[expected result]
您的查询没有 return 名为 last_updated
的列中的结果。
那是因为你正在进行聚合操作。
我认为查询编辑的 return 列称为 max
.
这可能有效:
String lastUpdated = queryResult.getString("max");
或者您可以使用别名:
ResultSet queryResult = st.executeQuery("SELECT MAX(customer.last_updated) as last_updated from customer");