giozh
giozh

Reputation: 10068

MySql and JDBC: change charset of a single column to utf8mb4

I have an "old" database (in utf 8) where I read and write on by using JDBC. Now, I must be able to also store emoji into a column of my table. I have changed the charset of involved columns to utf8mb4:

ALTER TABLE
myTable
CHANGE column_name column_name
longtext
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci
NOT NULL;

However, when I try to insert an emoticon into that column, I get the famous error

java.sql.SQLException: Incorrect string value: '\xF0\x9F\x91\x8D\xF0\x9F...'

Should I convert entire database, or am I doing something wrong?

Upvotes: 0

Views: 1485

Answers (1)

Rick James
Rick James

Reputation: 142453

Need to connect with utf8mb4 to get 👍, etc.

?useUnicode=yes&characterEncoding=UTF-8 in the getConnection() call.

As a fallback, execute SET NAMES utf8mb4 after connecting. (See Comment.)

"For Connector/J 8.0.12 and earlier: In order to use the utf8mb4 character set for the connection, the server MUST be configured with character_set_server=utf8mb4; if that is not the case, when UTF-8 is used for characterEncoding in the connection string, it will map to the MySQL character set name utf8, which is an alias for utf8mb3.'

Upvotes: 2

Related Questions