且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

彻底解决 MYSQL: Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x80\xE3\x80...' for column 'show_content' at row 1

更新时间:2022-08-15 22:40:11

Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x80\xE3\x80...' for column 'show_content' at row 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
    ... 64 more

今天在爬取文章的时候,在将数据插入mysql数据库的时候,出现了Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1 这个错误,Google了下发现原来是因为数据库编码问题导致的,原因在于我们的评论数据中存在emoj表情,而这些表情是按照四个字节一个单位进行编码的,而我们通常使用的utf-8编码在mysql数据库中默认是按照3个字节一个单位进行编码的,正是这个原因导致将数据存入mysql数据库的时候出现错误,那么这个问题我们应该怎么解决呢?

分为下面三个步骤来解决:

    (1)修改mysql数据库的编码为uft8mb4
    (2)修改数据表的编码为utf8mb4
    (3)修改连接数据库的连接代码

其中:

(1)修改mysql数据库的编码为uft8mb4

ALTER SCHEMA ak47_cms DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci ;

修改方法:
参看:http://blog.csdn.net/poice00/article/details/52129351

(2):修改数据表的编码为utf8mb4
执行命令:ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4;

(3):修改连接数据库的连接代码