记录一次数据库插入错误:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x8E \xF0...' for column

记录一次数据库插入表情时的报错解决方式

错误复现:在向数据库插入类似表情huaji doge 😄 😱 这样的数据时,发生了错误,示例如下:

[ERROR 06-10 17:34:08 sor.CommentProcessor 363] Can not add comment on article
org.b3log.latke.service.ServiceException: org.b3log.latke.repository.RepositoryException: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x8E \xF0...' for column 'commentContent' at row 1
	at org.b3log.solo.service.CommentMgmtService.addArticleComment(CommentMgmtService.java:340)
	at org.b3log.solo.service.CommentMgmtService_$$_jvst268_4e._d0addArticleComment(CommentMgmtService_$$_jvst268_4e.java)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)

重点在于这一句报错信息:Incorrect string value: '\xF0\x9F\x91\x8E \xF0...' for column 'commentContent' at row 1

原因是是数据库编码问题导致的,原因在于我们的评论数据中存在emoj表情,而这些表情是按照四个字节一个单位进行编码的,而我们通常使用的utf-8编码在mysql数据库中默认是按照3个字节一个单位进行编码的,正是这个原因导致将数据存入mysql数据库的时候出现错误。

解决方法:

  • 修改插入的该表中相应字段以及该表的数据编码为utf8mba
    示例:CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci
  • 修改数据库服务器的配置为utf8mb4(在mysql的配置文件{mysql}.cnf添加character_set_server = utf8mb4

经过上述步骤应该就能插入该行数据啦。

参考自:https://blog.csdn.net/hzw19920329/article/details/55670782


标题: 记录一次数据库插入错误:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x8E \xF0...' for column
文章作者: lanlonus
文章链接: https://www.louislan.com/articles/2021/06/10/1623318777087.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hi I'm LouisLan
    评论
    2 评论
    lonuslan 2021-06-12 22:58
    回复» @john_louis

    小伙子,你太天真了,层级评论写好了么?//TODO list😄

    john_louis 2021-06-12 22:56

    🎉️ 完结撒花了!

avatar

取消