Insert emoji does not work with spring-boot and MariaDB

后端 未结 4 583
北恋
北恋 2021-01-19 10:40

I would like to insert emoji like

相关标签:
4条回答
  • 2021-01-19 10:49

    For Spring boot with tomcat embedded use:

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

    0 讨论(0)
  • 2021-01-19 11:00

    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.

    0 讨论(0)
  • 2021-01-19 11:08

    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.

    0 讨论(0)
  • 2021-01-19 11:14

    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

    0 讨论(0)
提交回复
热议问题