gpt4 book ai didi

java - JDBC 错误 : operator does not exist: date = integer

转载 作者:行者123 更新时间:2023-11-29 12:28:58 25 4
gpt4 key购买 nike

    String checkAvailable_flight = String.format("SELECT Flightid, flightdate,"
+ " origin, destination FROM flight"
+ " WHERE Flightdate::Date = %s AND origin = %s"
+ " AND destination = %s;", date_, origin_, destination_);

ResultSet rs = stmt.executeQuery(checkAvailable_flight);

if (!rs.next()) {

System.out.println("no data inserted");
} else {

do {
int flightid = rs.getInt("flightid");
String date = rs.getString("flightdate");
String origin = rs.getString("origin");
String destination = rs.getString("destination");

System.out.printf("%-10d %5s %5s %7s\n",flightid, date, origin, destination);

} while (rs.next());
}

发生错误:

SQLException : ERROR: operator does not exist: date = integer
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 86
SQLState : 42883
SQLCode : 0

你好,我在 JDBC 上工作,想执行 sql 查询并打印出表..但是我得到了上面的错误..

我尝试以另一种方式转换航类日期,例如:

CAST(Flightdate AS TEXT) LIKE '2013-04-12%' 

但是错误还是出现了....

任何建议将不胜感激..

最佳答案

我猜您的日期可能被替换为未加引号,例如 2012-01-01 而不是 '2012-01-01'2012-01-01 是一个整数数学表达式,结果为数字 2010,因此您将日期与整数进行比较。您需要引用您的日期,或者更好的是,使用适当的准备好的语句。

为什么使用预处理语句?

为了证明我认为您的代码存在问题,我认为您正在这样做:

regress=> SELECT DATE '2012-03-12' = 2012-03-12;
ERROR: operator does not exist: date = integer
LINE 1: SELECT DATE '2012-03-12' = 2012-03-12;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

观察:

regress=> \x
Expanded display is on.
regress=> SELECT
2012-03-12 AS unquoted,
pg_typeof(2012-03-12) AS unquotedtype,
'2012-03-12' AS quoted,
pg_typeof('2012-03-12') AS quotedtype,
DATE '2012-03-12' AS typespecified,
pg_typeof(DATE '2012-03-12') AS typespecifiedtype;
-[ RECORD 1 ]-----+-----------
unquoted | 1997
unquotedtype | integer
quoted | 2012-03-12
quotedtype | unknown
typespecified | 2012-03-12
typespecifiedtype | date

(1 row)

如果您不使用准备好的语句,请将 %s 替换为 DATE '%s',但使用准备好的语句。

能否在格式化后添加一条语句打印checkAvailable_flight的内容,然后将其输出粘贴到此处以确认或反驳我的猜测?

关于java - JDBC 错误 : operator does not exist: date = integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073084/

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