微信获取的用户昵称nickname中带有emoji表情,转换成字符码后是这种形式“\xF0\x9F\x91\x8D\xE6\x94...”, 直接保存可能出现以下错误 Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for column 'review' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) 经检查原因:
UTF-8编码有可能是两个、三个、四个字节。Emoji表情是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去。 步奏1:修改my.ini 文件 [mysqld] character-set-server=utf8mb4
步奏2:在Connector/J的连接参数中,不要加characterEncoding参数。 不加这个参数时,默认值就时autodetect。 jdbc.driver_class com.mysql.jdbc.Driver jdbc.url jdbc\:mysql\://127.0.0.1\:3306/jlbmg?useUnicode\=true #&characterEncoding\=utf8 jdbc.username root jdbc.password root
步奏3: 将已经建好的表也转换成utf8mb4
alter table `TABLE_NAME` convert to character set utf8mb4 collate utf8mb4_general_ci;
步奏4:将需要使用emoji的字段设置类型为utf8mb4: 命令:alter table `TABLE_NAME`modify column `COLUMN_NAME` text character set utf8mb4 collate utf8mb4_general_ci; 步奏5:重启mysql:
开始->运行->cmd |