Add Chinese garbled characters to connect to mysql using java _ MySQL

Source: Internet
Author: User
Add Chinese garbled characters to connect to mysql using java bitsCN.com

Add Chinese data to mysql in java,

If the characters contain Chinese characters, the garbled characters are first thought to be due to inconsistent character encoding, so the database and table attributes are viewed.

The character set of the table is also utf8. if you do not want to set it, I will use utf8 as an example here.

View the mysql encoding.

# Show variables like 'character %'

Several encoding formats are not utf8. modify

Character_set_client gbk
Character_set_connection gbk
Character_set_database utf8
Character_set_filesystem binary
Character_set_results gbk
Character_set_server utf8

Character_set_system utf8

Character_sets_dir C:/Program Files/MySQL Server 5.5/share/charsets/

Change the encoding format

SET character_set_client = 'utf8 ';
SET character_set_connection = 'utf8 ';
SET character_set_results = 'utf8'; you can change the value if you do not,

You can also directly modify the C:/Program Files/MySQL Server 5.5/my. ini file.

Change default-character-set to utf8. Note that it is utf8 instead of UTF-8. Otherwise, the service restarts with 1667 error. restart the service and query the encoding again,

# Show variables like 'character %'

Displayed

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 C:/Program Files/MySQL Server 5.5/share/charsets/
It's all about utf8. when I add Chinese characters to mysql again, this time it's not a garbled code, but an error is reported in the program,

Incorrect string value: '/xE8/xBF/x87/xE5/x88/x86...' for column 'username' at row 1

I am not familiar with this, but I still found the answer. mysql can also encode the table fields,

Show full columns from tablename, use this command to view the character encoding of table fields

If the Collation column is not utf8, modify

Alter table change field varchar (100) character set utf8 collate utf8_general_ci not null default '';
The following is my settings for this table,

Alter table user change username varchar (100) character set utf8 collate utf8_general_ci not null default '';
Alter table user change password varchar (100) character set utf8 collate utf8_general_ci not null default '';
Alter table user change nickname varchar (100) character set utf8 collate utf8_general_ci not null default '';

This is how I connect to mysql.

Conn = DriverManager
. GetConnection (
"Jdbc: mysql: // localhost: 3306/test? UseUnicode = true & characterEncoding = utf8 ",
"Username", "password ");

The Chinese name is successfully added.

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.