JAVA write Mysql Chinese garbled solution, java write mysql garbled

Source: Internet
Author: User

JAVA write Mysql Chinese garbled solution, java write mysql garbled
Method 1:
When the page value contains Chinese characters, you can use the filter to compile it. However, it is easy to use System. out. println to output Chinese characters and insert it into Mysql. It is garbled. (Chao, your free space may also be the problem)

What should we do? Do the following:

1. When creating a database:

CREATEDATABASE 'db'
CHARACTERSET 'utf8'
COLLATE 'utf8 _ general_ci ';


2. When creating a table:

CREATETABLE 'tablea '(
'Id' varchar ( 40) NOTNULLdefault '',
'Userid' varchar ( 40) NOTNULLdefault '',)
ENGINE = InnoDB default charset = utf8;

3. When setting the URL:

Jdbc: mysql: /// localhost: 3306/database? UseUnicode = true & amp; characterEncoding = UTF-8

Method 2:

The default MySQL encoding is latin1.

We can see that although we can add and display Chinese characters normally when using the database, Chinese characters cannot be displayed normally when using the program to connect to the database. Therefore, we need to modify the default MySQL encoding, edit my. the ini (MySQL configuration file) file modifies the encoding.

Set the default Character Set of MySQL to utf8. Find the client configuration [client] and add it below.

Default-character-set = utf8

Find the server configuration [mysqld] and add it below

Default-character-set = utf8

Set the MySQL database to run in utf8 encoding and use utf8 encoding when connecting to the MySQL database.

Stop and restart MySQL

Net stop mysql

Net start mysql

Reconnect to the database and view the encoding and data table content.

Looking back at the display of the database, we will be very surprised to find out why the display is garbled, which is mainly related to the command line encoding in windows, view the current code page of the attribute> option on the command line: 936 (ANSI/OEM-Simplified Chinese GBK)
That is to say, the command line uses GBK encoding, while we use utf8 to add it during program connection, so garbled characters may appear, now let's change the client encoding to gb2312 or gbk.

Now we can see that the Chinese language is normally displayed (mainly because utf8 also supports Chinese), so when we use the command line tool to connect to the database, we 'd better change the client encoding, if you use the GUI, It is unnecessary. After modifying the client encoding, the program will still be displayed normally (the above two points have been tested)

So if you want to display Chinese characters in the program, you can use utf8, gb2312, and gbk encoding, however, if you want to add or view Chinese data in the command line, you need to set the client encoding to gb2312 or gbk. It is still related to the CMD encoding.

Related Article

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.