JAVA默认是UTF8编码的,按照JDBC文档描述:
To use 3-byte UTF8 with Connector/J set characterEncoding=utf8 and set useUnicode=true in the connection string.
因此连接数据库的字符串描述符应该为:
jdbc:mysql://localhost/DB_NAME?user=USER_NAME&password=PASSWORD&useUnicode=true&characterEncoding=utf8
但含有EMOJI的字符串使用的是UTF8M4编码,即最长为4个字节的变长编码,而不是3个,使用上述描述符插入数据库时会报错:
java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x92\x94’ for column ‘XXX’ at row 1
根据JDBC文档:
To use 4-byte UTF8 with Connector/J configure the MySQL server with character_set_server=utf8mb4. Connector/J will then use that setting as long as characterEncoding has not been set in the connection string. This is equivalent to autodetection of the character set.
此时应该去掉描述符中的characterEncoding参数,变为:
jdbc:mysql://localhost/DB_NAME?user=USER_NAME&password=PASSWORD&useUnicode=true
同时,Mysql的配置文件my.ini中应该修改character_set_server参数:
重启Mysql即可。