1. 程式人生 > 資料庫 >解決emoji表情插入資料庫報錯問題

解決emoji表情插入資料庫報錯問題

在富文字中新增emoji表情,再存入資料資料時,會報sql異常

在網上找了一些方法,按照步驟修改了資料庫、表和對應欄位的編碼格式為utf8mb4,但是還是報錯。

org.springframework.jdbc.UncategorizedSQLException: 
### Error updating database.  Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8C\xB9\xF0\x9F...' for column 'content' at row 1
### The error may involve com.xxx.mapper.xxxMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO xxxTable(id,title,content,create_by,update_by) VALUES( ?,?,?)
### Cause: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8C\xB9\xF0\x9F...' for column 'content' at row 1
; uncategorized SQLException; SQL state [HY000]; error code [1366]; Incorrect string value: '\xF0\x9F\x8C\xB9\xF0\x9F...' for column 'content' at row 1; nested exception is java.sql.SQLException: Incorrect string value: '\xF0\x9F\x8C\xB9\xF0\x9F...' for column 'content' at row 1
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89)

又繼續百度,發現了一種更為便捷的方式,直接在工程的application.properties檔案裡連線資料庫的JDBC配置裡,新增SQL編碼初始化的程式碼段。

spring.datasource.druid.connection-init-sqls=set names utf8mb4;

完美解決emoji表情無法插入的問題。