Why not use UTF-8 coding in MySQL to explain in detail
MySQL UTF-8 encoding
MySQL has supported UTF-8 since version 4.1, which was 2003, although the current popular UTF-8 standard (RFC 3629) was specified after that. This is why UTF-8 in MySQL is inconsistent with UTF-8 in our daily development, which leads to some problems. MySQL's UTF-8 only supports a maximum of three bytes per character, whereas true UTF-8 is a maximum of four bytes per character.
problem recurrence
There are database tables as follows: utf8 encoding
Save a record to the database:
@Testpublic void testInsert() { User user = new User(); user.setUsername("\uD83D\uDE00 "); user.setPassword("123456"); userRepo.save(user);}
Here is only part of the code, it doesn't matter if you don't understand it, here is to insert a record into the user table. where username is\uD83D\uDE00.
Actually, it's an emoji.
Because MySQL's utf8 character set only supports Unicode in three-byte UTF-8 encodings, and emoji characters belong to four-byte encodings, the program is expected to run with errors. Run this code:
As expected, misreported.
solve problems
Although MySQL UTF-8 has a bug, MySQL (including mariadb) officials did not fix this bug, but instead supported the real UTF-8 through the "utf8mb4" re-release in 2010. Therefore, to solve this problem, you can only set MySQL database to utf8mb4 character set.
summary
This problem was also discovered because an emoji expression was saved when saving data. In fact, when I first started using MySQL, I found a utf8mb4, but I didn't know the difference between UTF8 and UTF8MB4. After this lesson, use MySQL to honestly set the character set to utf8mb4.
Well, the above is all the content of this article, I hope the content of this article has a certain reference value for everyone's study or work, thank you for your support.