3
    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%' 

但错误仍然发生....

任何建议都会不胜感激..

4

1 回答 1

13

我猜你的日期可能会被替换而不引用,比如2012-01-01而不是'2012-01-01'. 2012-01-01是导致 number 的整数数学表达式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)

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

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

于 2013-04-18T01:43:58.980 回答