Insert emoji does not work with spring-boot and MariaDB

纵然是瞬间 提交于 2019-12-02 02:06:57

spring-boot 2.0.0.RC2

mysql 5.7.14

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\xAD' for column 'title' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
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:2484)
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)
... 68 more

spring.datasource.connectionInitSql is not work for me

i see hikari.pool

then try

spring.datasource.hikari.connectionInitSql=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

it works so do

spring.datasource.hikari.connection-init-sql=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

ref https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby

OKAY, I found the problem.

The solution was to add

spring:
  datasource:
     connectionInitSql: "SET NAMES 'utf8mb4'" 

in the application.yml.

connectionInitSql is used by HikariCP when it open the connection.

For Spring boot with tomcat embedded use:

spring.datasource.tomcat.initSQL = SET NAMES 'utf8mb4'

When you are connecting, set the charset to utf8mb4. Please provide the connection details.

Use utf8mb4 on the column(s) in the table. Please provide SHOW CREATE TABLE so we can verify that the column, not just the table default is utf8mb4.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!