Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8F' for column 'tweetcontent' at row 1

I try to save twitter feeds in mysql database in the following table

 CREATE TABLE `tweets` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `tweetcontent` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

but the following error has appeared

            java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8F'
                             for column  'tweetcontent' at row 1 

Can anyone help me please?

like image 560
ali ali Avatar asked Nov 26 '14 11:11

ali ali


1 Answers

Already answered here

MySQL's utf8 permits only the Unicode characters that can be represented with 3 bytes in UTF-8. Here you have a character that needs 4 bytes: \xF0\x90\x8D\x83 (U+10343 GOTHIC LETTER SAUIL).

If you have MySQL 5.5 or later you can change the column encoding from utf8 to utf8mb4. This encoding allows storage of characters that occupy 4 bytes in UTF-8.

You may also have to set the server property character_encoding_server to utf8mb4 in the MySQL configuration file. It seems that Connector/J defaults to 3-byte Unicode otherwise:

For example, to use 4-byte UTF-8 character sets with Connector/J, configure the MySQL server with character_set_server=utf8mb4, and leave characterEncoding out of the Connector/J connection string. Connector/J will then autodetect the UTF-8 setting.

like image 154
prem Avatar answered Nov 14 '22 16:11

prem