Home  >  Article  >  Java  >  Solution to the problem of Chinese garbled characters when connecting to the database through jdbc in Java

Solution to the problem of Chinese garbled characters when connecting to the database through jdbc in Java

WBOY
WBOYforward
2023-04-26 14:04:081904browse

1. Use jdbc to connect to the database. When inserting into the database, the data in the data displays garbled characters and is "???"

Two solutions:

1. Modify the mysql configuration file on the server side, edit the my.cnf file, add a line character_set_server = utf8 under [mysqld], and then restart the mysql service

2. When using jdbc connection, specify Correct encoding, jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8

2. Why is character_set_server = utf8 configured? You don’t need to specify the encoding when connecting to jdbc

Check the official documentation. When jdbc connects to the database, it will first query the character_set_server value of the server and then determine the encoding used when connecting. To override the client's automatic detection encoding function, you can use the characterEncoding attribute

Document address: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-charsets .html

3. What is the character processing mechanism of MYSQL? Why does jdbc not specify the encoding or the server does not set character_set_server=utf8, which will cause garbled characters

Check it first MYSQL character set

Solution to the problem of Chinese garbled characters when connecting to the database through jdbc in Java

Explain these parameters:

character_set_client: The character set used by the client source data
character_set_connection: Connection layer character set
character_set_database: The default character set of the currently selected database
character_set_filesystem: Convert the file name on the os into this character set. The default binary does not do any conversion
character_set_results: Query result character set
character_set_server: The default internal operating character set
character_set_system: System metadata character set
character_sets_dir: The path of the character set

Modify the mysql configuration file on the server side and edit the my.cnf file , add a line character_set_server = utf8 under [mysqld], then restart the mysql service, and then query the character set:

Solution to the problem of Chinese garbled characters when connecting to the database through jdbc in Java

Then understand the character set conversion process:

Insert data:

The server converts the data from the client (character_set_client) character set to character_set_connection, and then converts the character_set_connection character set into the corresponding character set and stores it in the disk. This The corresponding character set is judged according to the following priority:

1. The character set set when creating the table

2. The character set set when creating the library

3. character_set_database character set

4. character_set_server character set

Query data:

The server converts the data from the character set stored in the disk into character_set_results Character set, return to the client

and then analyze

1. jdbc does not set characterEncoding=utf8, the server character_set_server is latin1, jdbc connects to the database with latin1 character set, and the database service The end converts latin1 to utf8 and then stores it on the disk (because the character set specified by character_set_connection and the creation table is both utf8), which causes garbled characters

2. JDBC does not set characterEncoding=utf8, and the server character_set_server is utf8, or jdbc sets characterEncoding=utf8, jdbc uses utf8 character set to connect to the database, and the database server stores it to disk in utf8. At this time, the data is normal

The above is the detailed content of Solution to the problem of Chinese garbled characters when connecting to the database through jdbc in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete