标签:
Java 获取到微信端传递过来的Emoji表情插入MySQL之后报错 , 错误信息如下
java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x92\x94‘ for column ‘name‘ at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581)
UTF-8编码有可能是两个、三个、四个字节。Emoju表情是4个字节,而MySQL的UTF8编码最多3个字节,所以数据插不 进去。解决方案就是:将MySQL的编码从UTF8转换成UTF8MB4 。
1:修改my.cnf
[mysqld]
character-set-server=utf8mb4
[mysql]
default-character-set=utf8mb4
修改后重启MySQL
2:命令
ALTER TABLE [TABLE_NAME] CONVERT TO CHARACTER SET UTF8MB4 COLLATE UTF8MB4_BIN;
标签:
原文地址:http://my.oschina.net/u/2367501/blog/491706