I use Mysql 5.7.18 and I faced an issue with emoji symbols when updating from JDBC
.
Table creation: body
is typed utf8
and localizations
is utf8mb4
CREATE TABLE `my_table` (
`body` TEXT NOT NULL,
`localizations` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
Now I use SequelPro
to insert data. To do it correctly I press Database -> View using encoding -> utf8mb4
. Insertion code:
INSERT INTO my_table(body, localizations) VALUES ('test', 'π₯');
Then I try to load it from jdbc. Here is connection string part currently used in project (formatting is used here only for convenient reading):
?useEncoding=true
&characterEncoding=UTF-8
It successfully loads localizations
from database. But updating table from JDBC returns error:
SQLException: Incorrect string value: '\xF0\x9F\x94\xA5"}...' for column 'localizations'
I suppose that's because of connection encoding. SequelPro
has a Show server variables
option and here is the diff between utf8
and utf8mb4
:
character_set_client = utf8mb4
character_set_connection = utf8mb4
character_set_results = utf8mb4
collation_connection = utf8mb4_general_ci
So I try to modify connection string:
?useEncoding=true
&characterEncoding=UTF-8
&sessionVariables=
character_set_client=utf8mb4,
character_set_connection=utf8mb4,
character_set_results=utf8mb4,
collation_connection=utf8mb4_general_ci
Still the same issue. Any advice please?
SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'
immediately after establishing your Connection. β Migrate