I am using PostgreSQL 9.1.4 with hstore and the PostgreSQL JDBC driver (9.1-901.jdbc4).
I am trying to use the contains operators (?
, ?&
, ?|
) in a PreparedStatement, however the ?
character is parsed as a variable placeholder. Can this character be escaped to send the correct operator in the query?
An example:
PreparedStatement stmt = conn.prepareStatement("SELECT a, b FROM table1 WHERE c ? 'foo' AND d = ?");
stmt.setInt(1, dValue);
stmt.executeQuery();
In this form the following example would raise an exception:
org.postgresql.util.PSQLException: No value specified for parameter 2.
Update:
After investigating the query parser in the pgjdbc driver this snippet seems to indicate that it is not possible to escape the ?
character. The questions that remain are:
- Is there anything in the JDBC spec which allows a
?
to be escaped and be anything other than a parameter placeholder? - Is there any better work around for this issue than just using plain Statements with variables manually inserted into the query string?
d = $1
work? – Binate$1
. – Hardison$1
,$2
, ...), I guess JDBC isn't one of them. – Binate