Insert emoji does not work with spring-boot and Ma

2019-03-02 13:55发布

I would like to insert emoji like

4条回答
Emotional °昔
2楼-- · 2019-03-02 14:04

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

查看更多
ら.Afraid
3楼-- · 2019-03-02 14:08

For Spring boot with tomcat embedded use:

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

查看更多
4楼-- · 2019-03-02 14:10

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.

查看更多
神经病院院长
5楼-- · 2019-03-02 14:11

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.

查看更多
登录 后发表回答