gpt4 book ai didi

java - 从 Java 代码调用 SQL 查询

转载 作者:行者123 更新时间:2023-11-30 08:42:41 24 4
gpt4 key购买 nike

我在 SQL Developer 中成功执行了这个 SQL 查询:

SELECT
COUNT(*) "All",
COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) "Critical",
COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) "Info",
COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) "Warning",
COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) "Error"
FROM
EVENT;

但是当我尝试以这种方式执行查询时出现错误

dbRequest.setPreparedStatement("SELECT "
+ " COUNT(*) 'All',"
+ " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"Critical\", "
+ " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"Info\", "
+ " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"Warning\", "
+ " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"Error\" "
+ " FROM "
+ " EVENT");

错误:

SQL exception: Cannot get object 923: ORA-00923: FROM keyword not found where expected

你知道我该如何解决这个问题吗?

最佳答案

您在请求中混淆了引号:

在第二行,您必须将 '' 替换为 "

dbRequest.setPreparedStatement("SELECT "
+ " COUNT(*) \"All\","
+ " COUNT(CASE WHEN TYPE = 'Critical' THEN 1 END) \"Critical\", "
+ " COUNT(CASE WHEN TYPE = 'Info' THEN 1 END) \"Info\", "
+ " COUNT(CASE WHEN TYPE = 'Warning' THEN 1 END) \"Warning\", "
+ " COUNT(CASE WHEN TYPE = 'Error' THEN 1 END) \"Error\" "
+ " FROM "
+ " EVENT");

关于java - 从 Java 代码调用 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com