How do I escape a literal question mark ('?') in a JDBC prepared statement

前端 未结 5 1835
醉酒成梦
醉酒成梦 2021-02-19 10:56

I\'d like to create a JDBC PreparedStatement like:

SELECT URL,LOCATE ( \'?\', URL ) pos FROM Links WHERE pageId=? ORDER BY pos ASC

Where the 1s

5条回答
  •  醉酒成梦
    2021-02-19 11:43

    Depending on the JDBC driver you are using you may be able to escape by adding another question mark e.g. if you're using PostgreSQL

    https://jdbc.postgresql.org/documentation/head/statement.html

    In JDBC, the question mark (?) is the placeholder for the positional parameters of a PreparedStatement. There are, however, a number of PostgreSQL operators that contain a question mark. To keep such question marks in a SQL statement from being interpreted as positional parameters, use two question marks (??) as escape sequence. You can also use this escape sequence in a Statement, but that is not required. Specifically only in a Statement a single (?) can be used as an operator.

提交回复
热议问题