hhrzc
hhrzc

Reputation: 2051

Invalid string value: '\ xD0...." for the column .... ". utf8 charset try used

In the spring project, I try to introduce Cyrillic characters into the database. But the database does not encode it.

I used the extended JpaRepository interface, method save (T t), and everything works correctly when I send English text;

when the program tries to save the entity with the Cyrillic, I get the exception "Invalid string value: '\ xD0 \ xA5 \ xD0 \ xB0 \ xD0 \ xB1 ..." for the column .... "

So encoding does not work.

My database character variables:

enter image description here

application.properties:

launchMode=cli

#Database settings
spring.datasource.url=jdbc:mysql://localhost:3306/mySecondBD?serverTimezone=UTC
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username=root
spring.datasource.password=password
#spring.datasource.driver-class-name=org.gjt.mm.mysql.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
spring.datasource.sql-script-encoding=UTF-8

Question:

Where more I need set charset encoding params?

Upvotes: 0

Views: 1277

Answers (4)

Sudarshan Nagre
Sudarshan Nagre

Reputation: 27

Used this query , it works for me... ALTER TABLE table_name MODIFY COLUMN column_name varchar(255)
CHARACTER SET utf8 COLLATE utf8_general_ci;

Upvotes: 1

Rick James
Rick James

Reputation: 142453

Hex D0A5D0B0D0B1 is the UTF-8 encoding for Cyrillic Хаб. So, using MySQL's utf8 (or utf8mb4) should work.

When you said '\ xD0 \ xA5 \ xD0 \ xB0 \ xD0 \ xB1 ...", you had extra spaces; were they in the output you saw?

Please provide SHOW CREATE TABLE page.

Somehow, you need to say that the client encoding is utf8. One way (in spring) is to put this in the application.yml:

 datasource:
     connectionInitSql: "SET NAMES 'utf8'"

Upvotes: 1

hhrzc
hhrzc

Reputation: 2051

In summary, I do next steps and all runs:

  • edit application.properties, how me advised

    spring.datasource.url=jdbc:mysql://localhost:3306/mySecondBD?useUnicode=yes&characterEncoding=UTF-8

  • changed own my table encode property(not only database):

    ALTER TABLE page CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE page CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

  • and I changed variable column into text:

    alter TABLE page MODIFY content TEXT(21844);

PS variables of the database:

| Variable_name            | Value                      |

| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |

Upvotes: 0

0gam
0gam

Reputation: 1423

Edit properties :

spring.datasource.url=jdbc:mysql://localhost:3306/mySecondBD?useUnicode=yes&characterEncoding=UTF-8

Check your variables :

mysql> show variables like 'char%';

Edit my.cnf :

vi /etc/my.cnf

[client]
default-character-set=utf8

Finally, you recheck your MySQL variables and confirm your query result.

Upvotes: 1

Related Questions