【问题标题】:Unable to store messages containing special characters无法存储包含特殊字符的消息
【发布时间】:2011-12-22 12:25:18
【问题描述】:

我正在尝试将以下推文存储到 MySQL 数据库中,但失败并出现以下异常。我如何解决它?任何帮助表示赞赏。我正在使用 utf8 字符集数据库,我认为消息中的 !??? 在这里有问题。

tweet=由于我的身高,我在 USS 被禁止乘车,我想坐在中间,以防万一发生意外,但我仍然爱我的身高!???

Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x8D\xF0\x9F...' for column 'message' at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)

【问题讨论】:

  • 我感觉到两个可能的问题。 1. ' 和 2. @。尝试从字符串中删除两者并尝试保存。
  • ' 对我来说似乎更有问题。您的更新语法是什么样的?我打赌你正在使用内联 SQL,它为 SQL 注入打开了大门。您应该真正使用参数。它将消除您的问题并提高代码的安全性。

标签: java mysql hibernate jpa


【解决方案1】:

在您的驱动程序 url 路径中使用以下参数

jdbc:mysql://<host>:<port>/<db>?useUnicode=true&characterEncoding=UTF-8

【讨论】:

  • 我尝试了那个驱动程序 url,但我仍然遇到同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 2021-01-08
  • 2012-02-05
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 2021-04-19
相关资源
最近更新 更多