如果没有PreparedStatement,你如何正确地逃避字符串?

ran*_*guy 1 postgresql encoding escaping jdbc

我正在使用Statement的executeUpdate和executeQuery.我在SQL查询中连接的字符串值至少可以包含'\字符和Unicode.

PreparedStatement似乎是自动执行此操作,但JDBC库中是否有一些实用程序函数可以转义任意字符串以用于SQL查询?

我遇到的示例错误:

org.postgresql.util.PSQLException: ERROR: unterminated quoted string at or near
Run Code Online (Sandbox Code Playgroud)

org.postgresql.util.PSQLException: ERROR: invalid Unicode escape
  Hint: Unicode escapes must be \uXXXX or \UXXXXXXXX.
Run Code Online (Sandbox Code Playgroud)

Jon*_*nas 5

不,它不是JDBC的一部分,它对于不同的数据库管理系统是不同的.您应该真正PreparedStatement用于带参数的查询.这样更安全,并且可以更好地执行,因为可以编译查询.

4.1.SQL语法 - PostgreSQL手册中的词法结构.

例如

以下不那么简单的例子用西里尔字母写下俄语单词"slon"(elephant):

U&"\0441\043B\043E\043D"
Run Code Online (Sandbox Code Playgroud)