Get the App
SLTechnology News&Howtos  ›  Database  › 

How to deal with the garbled code when java writes to mysql database

Shulou Source: shulou.com Published: 2022-06-01 20:49:33 09月17日 Update

The following content mainly brings you java to write mysql database how to deal with garbled code, the knowledge mentioned, and books slightly different, are professional and technical personnel in contact with users in the process, summed up, has a certain experience sharing value, hope to bring help to the majority of readers.

Ensure database tables are created with consistent coding:

When creating a database:

CREATE DATABASE `Db`

CHARACTER SET 'utf8 '

COLLATE 'utf8_general_ci';

NOTE: CHARACTER SET 'utf8 ': This is the setting character encoding;

COLLATE 'utf8_general_ci': Sets character sort encoding

You can set it on the command line: alter table tableA character set utf8

When creating tables:

CREATE TABLE `TableA` ( `ID` varchar(40) NOT NULL default ' ',

`UserID` varchar(40) NOT NULL default ' ', ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Note: Set table character encoding DEFAULT CHARSET=utf8

You can set it on the command line: alter table tableA charset utf

When setting URL:

jdbc:mysql://localhost:3306/database? useUnicode=true&characterEncoding=UTF-8

Note: here useUnicode=true&characterEncoding= UTF-8 can not be added here, the default useUnicode is true

characterEncoding automatically detects changes to the database currently in use

2. The above settings may be used incorrectly, which requires modifying the my.ini configuration file in mysql

Set MySQL default character set to utf8, find client configuration [client] and add below.

default-character-set=utf8

Find Cloud Virtual Machine Configuration [mysqld] Add below

default-character-set=utf8

Set MySQL database to run with utf8 encoding, use utf8 encoding when connecting MySQL database

Stop and restart MySQL

net stop mysql

net start mysql

Note: Find server configuration [mysqld] Add below: default-character-set=utf8 Adding here will cause the database to fail to start

Add default-character-set=utf8 alone

Reconnect database, view codes, data table contents

mysql> show variables like 'character%';

--------------------------+-----------------------------------------------+

| 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:\wamp\bin\mysql\mysql5.5.20\share\charsets\

As you can see from above, except| character_set_database |latin1 and character_set_server| latin1 is encoded by latin1 and the others are encoded by utf8, which leads to inconsistent encoding of client and server. The inserted data will still be garbled in Chinese.

Solution:

[mysqld] Add character_set_server=utf8 below and restart

[mysqld]

port=3306

character_set_server=utf8

#default-character-set=utf8

use the following command

mysql> show variables like 'character%';

+--------------------------+-----------------------------------------------+

| 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:\wamp\bin\mysql\mysql5.5.20\share\charsets\ |

+--------------------------+-----------------------------------------------

Here you can see that all the codes are consistent, so there will be no garbled code.

|

| f512ebc1-e587-4baf-bd17-42e27236621f | ??? | female | 2017-02-24 | 119 | jzz@itheima.com | ??,??,? java | VIP | ?? |

Chinese chaos before going in and out. The following is the result of the modification

| ff1355cb-b25c-4431-8f96-7f56875b582f |Liu Bei| female | 2017-02-24 | 119 |jzz@itheima.com|Eat, sleep, learn Java| VIP |beauty|

In the command prompt, the default is gbk encoding. To avoid garbled display, use set names gbk when using query statements.

For the above on java write mysql database appear garbled how to deal with, if you still need to know more can continue to pay attention to our industry push new, if you need to get professional answers, can contact the official website pre-sale after-sales, hope this article can bring you a certain knowledge update.

Tags: Data coding database garbled command character configuration consistency time processing specialty content can be passed datasheet server knowledge Chinese service different below Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi OPPO Reno NVidia MySQL