When we used JDBC to connect to the MySQL database server, we often had garbled characters. When we first learned it, our first thought was that the client would solve the problem by adding parameters after the connection URL

jdbc:mysql://localhost:3306/testdb? useUnicode=true&characterEncoding=UTF-8Copy the code

After coming out to work, I also used the same method, but the technical manager saw my method and said that we did not, I do not know why, he said that problems should be solved from the server side of the database, can solve the server side to solve the server side.

Use ‘show variables like’ %char% ‘when there are no changes to my.ini; ‘The query looks like this:

Variable_name Value
character_set_client gbk
character_set_connection gbk
character_set_database latin1
character_set_filesystem binary
character_set_results gbk
character_set_server latin1
character_set_system utf8
character_sets_dir D: \ tools/mysql/mysql – 5.5.57 – winx64 \ share \ charsets \

Add default-character-set = utf8 under [mysqld] and [client] respectively

[mysqld]
# set character set to UTf8
default-character-set = utf8

[client]
Set the client character set
default-character-set = utf8Copy the code

MySQL: unknown variable ‘default-character-set=utf8’ MySQL: unknown variable ‘default-character-set=utf8’

I use the 5.5.57 version. This version does not support directly configuring ‘default-character-set = utf8’ in my. Change it to ‘loose-default-character-set = UTf8’ and you’ll be fine

[mysqld]
# set character set to UTf8
loose-default-character-set = utf8

[client]
Set the client character set
loose-default-character-set = utf8Copy the code

I’m not going to get an error, but I’m going to look at the code

Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database latin1
character_set_filesystem binary
character_set_results utf8
character_set_server latin1
character_set_system utf8
character_sets_dir D: \ tools/mysql/mysql – 5.5.57 – winx64 \ share \ charsets \

‘character_set_server’ is the same as’ latin1 ‘encoding.’ character_set_server ‘is the same as’ latin1′ encoding. [mysqld] add character-set-server = utf8 to mysqld

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 D: \ tools/mysql/mysql – 5.5.57 – winx64 \ share \ charsets \

All corrected and done!!