表情存储异常--mybatis抛出异常(java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1)
文章参考
https://blog.csdn.net/junsure2012/article/details/42171035
https://www.cnblogs.com/WangYunShuaiBaoLe/p/9055215.html
https://www.jb51.net/article/112879.htm
背景
iOS端测试时发现,在备注一栏输出emoji表情,保存时出现系统异常
java项目架构 spring-boot+mybatis+德鲁伊连接池
现象
抛出 java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1
定位
1、数据库字段、表、数据库、mysql的编码需要设置成utf8mb4
2、数据库连接设置编码
show variables like "%char%";
解决
1、设置数据库
1)修改字段字符集
ALTER TABLE table_name CHANGE column_name VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2)设置表的字符集
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
3)设置数据库的字符集
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
4)修改数据库应用字符集
找到linux下的mysql位置
$ whereis mysql 找到位置
$ vi my.cnf 【这里有my.ini,如果只有my-default.ini,则复制一份并命名为my.ini】
[增加或修改]
[client]
# 客户端来源数据的默认字符集
default-character-set = utf8mb4
[mysqld]
# 服务端默认字符集
character-set-server=utf8mb4
# 连接层默认字符集
collation-server=utf8mb4_unicode_ci
[mysql]
# 数据库默认字符集
default-character-set = utf8mb4
$ service mysqld restart 重启服务即可
2、设置编码
在命令行中输入,但是这个只在当前会话起作用
set character_set_database=utf8;
set character_set_server=utf8;
3、修改连接池属性(设置会话字符集)
<property name="connectionInitSqls"> <list>
<value>set names utf8mb4</value>
</list>
</property>

注: set names utf8mb4; 命令会将 character_set_client、character_set_connection、character_set_results 3个会话字符集相关变量均设置为 utf8mb4,以保证写入或者读出的数据使用 utf8mb4 字符集进行解释。
并且
jdbc.url=jdbc:mysql://localhost:/database?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
特别说明其中的jdbc.url配置:如果你已经升级好了mysql-connector,其中的characterEncoding=utf8可以被自动被识别为utf8mb4(当然也兼容原来的utf8),
而autoReconnect配置我强烈建议配上,我之前就是忽略了这个属性,导致因为缓存缘故,没有读取到DB最新配置,导致一直无法使用utf8mb4字符集,多么痛的领悟!!
亲测可以
utf8与utf8mb4说明:
UTF- 8:Unicode Transformation Format-8bit,允许含BOM,但通常不含BOM。是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24为(三个字节)来编码。
UTF-8包含全世界所有国家需要用到的字符,是国际编码,通用性强。UTF-8编码的文字可以在各国支持UTF8字符集的浏览器上显示。如果是UTF8编码,则在外国人的英文IE上也能显示中文,他们无需下载IE的中文语言支持包。
UTF8MB4:MySQL在5.5.3之后增加了utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode。
表情存储异常--mybatis抛出异常(java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1)的更多相关文章
- mybatis抛出异常(java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1)
文章参考 https://blog.csdn.net/junsure2012/article/details/42171035 https://www.cnblogs.com/WangYunShuai ...
- 彻底解决:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1
转载:https://blog.csdn.net/qq_31122833/article/details/83992085
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'nick' at row 1 mysql报错 ...
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column 'Content' at row 1
在尝试将 emoji 表情符号 插入MySQL数据库时,遇到以下错误信息: ### The error occurred while setting parameters ### SQL: INSER ...
- mysql 无法存储表情字符 java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xBE",...' for column 'XXXX' at row 1
1.变更字段类型 ALTER TABLE api_log MODIFY COLUMN remark longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_uni ...
- java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8E' for column 'name' at row 1
我的错误案例: ,这个后台插不进去,就姓名那栏的中文编码问题. 遇到这个错误,应该是创建表的时候没有设置好编码,这个错误不用多想,我也试过在更改表那里设置编码,但还是不行,还是有残留 直接drop t ...
- MySql中报错:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xBB' for column
问题描述: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xBB' for column 'nickName' at row ...
- mysql: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...'
插入数据出现问题,因为包含了特殊字符. 现象: 插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试**插入数据...“,在console中insert是正常的,但是使用java代码 ...
- 【MySQL】java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xB3' for column
问题原因: 输入内容包含特殊字符,MySQL 中的列不支持. 解决方法 多数是修改 MySQL 的数据库和表结构,CHARSET 改为 utf8mb4,但本人测试还是不能传入 emoji. 后来在代码 ...
随机推荐
- IE8浏览器官方下载 包含Windows中繁英文各个版本
当你的Windows XP安裝IE8时提示"此安装不支持您的操作系统的当前语言",或者安装IE8,在语言包安装时报错,可以尝试使用先安装英文版本,再安装多国语言包的方式操作. 20 ...
- ASP.NET Identity 一 (转载)
来源:http://www.cnblogs.com/r01cn/p/5194257.html 注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的A ...
- Shaping Regions(dfs)
Shaping Regions Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 124 Solved: 39[Submit][Status][Web B ...
- Visual Studio 2015速递(4)——高级特性之移动开发
系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...
- String全面解析
前言 public class Test { public static void main(String[] args) { String a = "abc"; String b ...
- 【Java入门提高篇】Day29 Java容器类详解(十一)LinkedHashSet详解
当当当当当当当,本来打算出去浪来着,想想还是把这个先一起写完吧,毕竟这篇的主角跟我一样是一个超级偷懒的角色——LinkedHashSet,有多偷懒?看完你就知道了. 本篇将从以下几个方面对Linked ...
- beta冲刺随笔集
团队成员 郑西坤 031602542 (队长) 陈俊杰 031602504 陈顺兴 031602505 张胜男 031602540 廖钰萍 031602323 雷光游 031602319 吴志鸿 03 ...
- ffmpeg文件切片
先用ffmpeg把abc.mp4文件转换为abc.ts文件: ffmpeg -y -i abc.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb ...
- WFE和WFI的区别
1. 概念: WFI(Wait for interrupt)和WFE(Wait for event)是两个让ARM核进入low-power standby模式的指令,由ARM architecture ...
- ws协议的配置
server { listen 80; server_name 域名或IP; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 4 ...