Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report