【问题标题】:Insert the Apostrophe using jdbctemplate使用 jdbctemplate 插入撇号
【发布时间】:2016-07-14 12:33:42
【问题描述】:

我正在使用spring boot jdbctemplate将记录插入数据库。但是我发现使用 jdbctemplate.update(query) ,其中 query 是用于在数据库中插入记录的字符串。

我想在表格列中插入撇号和特殊字符,但我得到 BadSqlGrammarException。

例如,我的查询是这样形成的 VALUES('abc 的例子')

我看到 Escaping a single quote when using JdbcTemplate ,在 cmets 中写到使用创建 PreparedStatement 的 jdbctemplate.update(query,Object[] args) ,但我有从每个模块调用 jdbctemplate.update() 方法的通用方法的代码。

我想将它用于插入和更新操作,目前我正在使用 jdbctemplate.update(query)

【问题讨论】:

    标签: spring spring-boot spring-data


    【解决方案1】:

    在 SQL 中,单引号通过加倍来“转义”:

    VALUES( 'abc''s example ')
    

    但改用PreparedStatement好多更好。

    【讨论】:

    • 其他字符集呢?它们会自动处理吗
    • @Chetan:哪些“其他”角色?单引号是唯一需要在字符常量中转义的(在符合标准的 DBMS 中)
    • 我的意思是其他特殊字符
    • @Chetan:还有哪些“其他”特殊字符?
    • 所有其他字符,如 * 、 ASCII 、 + 、 - 、 .!@#$%^&*(()
    猜你喜欢
    • 2011-03-11
    • 2017-07-24
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    相关资源
    最近更新 更多