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%'
但错误仍然发生....
任何建议都会欣赏它..
答案 0 :(得分:8)
我猜你的日期可能在没有引用的情况下被替换,例如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
的内容,然后将其输出粘贴到此处以确认或反驳我的猜测吗?