I was experiencing the same problem as the OP, where emojis were being entered into strings that I needed to store in MySQL. What worked for me was a combination of four things:
- Added "?useUnicode=true&characterEncoding=utf8" to the end of my jdbc: connect string
- I had been using Class.forName("com.mysql.cj.jdbc.Driver") to register the JDBC driver, and that doesn't seem to work with UTF8MD4. I changed this to:
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
- Altered my table so the default character set for the table is UTF8MD4:
ALTER TABLE mytable DEFAULT CHARACTER SET utf8mb4
- Altered the fields of my table that could contain emoji strings to be utf8md4:
SET myfield utf8mb4 COLLATE utf8mb4_unicode_ci;
I did not seem to have to alter the entire database to be UTF8MD4, just the table and the fields of the table that would store these emoji strings.