I am using Emoji character in my project. That characters are saved (??) into mysql database. I had used database Default collation in utf8mb4_general_ci
.
It sh
1) Database: Change Database default collation as utf8mb4
.
2) Table: Change table collation as CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
.
Query:
ALTER TABLE Tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
3) Code:
INSERT INTO tablename (column1, column2, column3, column4, column5, column6, column7)
VALUES ('273', '3', 'Hdhdhdh
The main point hasn't been mentioned in the above answers that,
We need to pass query string with the options "useUnicode=yes"
and "characterEncoding=UTF-8"
in connection string
Something like this
mysql://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE_NAME?useUnicode=yes&characterEncoding=UTF-8
Emoji support for application having tech stack - mysql, java, springboot, hibernate
Apply below changes in mysql for unicode support.
ALTER DATABASE <database-name> CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE <table-name> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
DB Connection - jdbc url change:
jdbc:mysql://localhost:3306/<database-name>?useUnicode=yes&characterEncoding=UTF-8
Note - If the above step is not working please update mysql-connector version to 8.0.15. (mysql 5.7 works with connector version 8.0.15 for unicode support)
The simplest solution what works for me is to store the data as json_encode.
later when you retrieve just make sure you json_decode it.
Here you don't have to change the collation or the character set of the database and the table.