无法存储包含特殊字符的消息

3

我正在尝试将以下推文存储到MySQL数据库中,但是遇到了以下异常。如何解决?非常感谢您的帮助。我正在使用utf8字符集数据库,我认为消息中的!???可能会导致问题。

推文=我因为身高被禁止乘坐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. **@**。尝试从字符串中删除它们并尝试保存。 - ManuPK
1
“'”对我来说似乎更有问题。你的更新语法是什么样子?我打赌你正在使用内联SQL,这会导致SQL注入的风险。你应该使用参数代替。这将消除你的问题并提高代码的安全性。 - xQbert
1个回答

2

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

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

我尝试了那个驱动程序的网址,但仍然遇到同样的问题。 - diya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接