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.