Java 獲取到微信端傳遞過來的Emoji表情插入MySQL以後報錯 , 錯誤信息以下java
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 。mysql
1:修改my.cnfsql
[mysqld]微信
character-set-server=utf8mb4編碼
[mysql]code
default-character-set=utf8mb4
server
修改後重啓MySQLstring
2:命令io
ALTER TABLE [TABLE_NAME] CONVERT TO CHARACTER SET UTF8MB4 COLLATE UTF8MB4_BIN;
class