java - JDBC ERROR: operator does not exist: date = integer -
string checkavailable_flight = string.format("select flightid, flightdate," + " origin, destination flight" + " flightdate::date = %s , origin = %s" + " , destination = %s;", date_, origin_, destination_); resultset rs = stmt.executequery(checkavailable_flight); if (!rs.next()) { system.out.println("no data inserted"); } else { { 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()); }
error(s) occurred:
sqlexception : error: operator not exist: date = integer hint: no operator matches given name , argument type(s). might need add explicit type casts. position: 86 sqlstate : 42883 sqlcode : 0
hello, working on jdbc , wanna execute sql query , print out table ..but got error above..
i try casting flightdate in way, like:
cast(flightdate text) '2013-04-12%'
but error still occurred....
any suggestion appreciate it..
i'm guessing dates being substituted in without quoting, 2012-01-01
instead of '2012-01-01'
. 2012-01-01
integer mathematical expression results in number 2010
, you're comparing date integer. need quote dates, or better, use proper prepared statements.
why used prepared statements?
- it's easier right
- lil' bobby tables
- wikipedia: sql injection
to demonstrate think code's problem is, think you're doing this:
regress=> select date '2012-03-12' = 2012-03-12; error: operator not exist: date = integer line 1: select date '2012-03-12' = 2012-03-12; ^ hint: no operator matches given name , argument type(s). might need add explicit type casts.
observe:
regress=> \x expanded display on. regress=> select 2012-03-12 unquoted, pg_typeof(2012-03-12) unquotedtype, '2012-03-12' quoted, pg_typeof('2012-03-12') quotedtype, date '2012-03-12' typespecified, pg_typeof(date '2012-03-12') typespecifiedtype; -[ record 1 ]-----+----------- unquoted | 1997 unquotedtype | integer quoted | 2012-03-12 quotedtype | unknown typespecified | 2012-03-12 typespecifiedtype | date (1 row)
if won't use prepared statements, replace %s
date '%s'
, please use prepared statements.
can add statement print contents of checkavailable_flight
after formatting, paste output here confirm or refute guess?
Comments
Post a Comment