MySQL insert emoji expression error SQLException: Incorrect string value of the two solutions

Excerpt from: https://blog.csdn.net/dmw412724/article/details/81119325

 

The reason: mysql the UTF-8 support only three bytes of storage, and the general character is three bytes, but emoji expression is 4 bytes, so the store can not.

The first scenario: modify the database character set table

  The mysql To solve this problem, after the 5.5.3 version in favor of the 4 bytes of storage utf8 character, character set utf8mb4. This is equivalent to the difference between windowsX64 and windowsX86, and is compatible with 64-bit 32-bit, that is, He said utf8mb4 access utf8 is no problem.

  Then it should be done in the field and expressive way, set this field to utf8mb4

 ALTER TABLE table_name MODIFY  colum_name  VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci

  However, you also need to set the character set in the table below

 ALTER TABLE table_name CHARSET=utf8mb4;

  Of course, you may also need to set up this database utf8mb4 ah

 SET NAMES utf8mb4

  Then start up.

  But local server and database connections how to do?

jdbc.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8

  Here we wanted to change this utf-8 utf8mb4 it?
  Seemingly can not change ah, database-driven on these codes can not change ah, a change will go wrong, but nothing, it is compatible, for the jdbc driver for.
  Since nothing , then test store it.
  I do not know the principle, just know the phenomenon.
  So then there will be two phenomena,
  first: You can store the expression.
  Second: an unknown reason, you still can not store expression.
  For students of the second case, I want to tell you is that when you go before operating these fields need to add expression, insert this sql statement.

SET NAMES utf8mb4

  It can be.

  That each insertion ah ah modifications are set before the next would be no problem.

 

The second option: Filter emoji expression through Java code reload the database

  Of course, there is a more easy way, then more than a few paragraphs when I did, what things do not change, we look at the issue from another angle, in fact, we filter out to look at the time of deposit, do not worry subsequent take a variety of species compatible with a variety show.

  

1 <!-- https://mvnrepository.com/artifact/com.vdurmont/emoji-java -->
2 <dependency>
3     <groupId>com.vdurmont</groupId>
4     <artifactId>emoji-java</artifactId>
5     <version>4.0.0</version>
6 </dependency>

  This bag, there is this method

1 String string = EmojiParser.removeAllEmojis(param);

  ok. This can get to keep.

Guess you like

Origin www.cnblogs.com/nemowang1996/p/11910342.html