How to solve Chinese garbled characters written into MySQL using Java

Source: Internet
Author: User
Tags mysql code

How to solve Chinese garbled characters written into MySQL using Java

How to solve the problem of garbled characters in MySQL written in Java:

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.

  1. Mysql> show variables like 'character % ';
  2. + -------------------------- +
  3. | Variable_name | Value |
  4. + -------------------------- +
  5. | Character_set_client | latin1 |
  6. | Character_set_connection | latin1 |
  7. | Character_set_database | latin1 |
  8. | Character_set_filesystem | binary |
  9. | Character_set_results | latin1 |
  10. | Character_set_server | latin1 |
  11. | Character_set_system | utf8 |
  12. | Character_sets_dir | D: \ MySQL \ share \ charsets \ |
  13. + -------------------------- +
  14. Create a data table and insert data
  15. Mysql> use test;
  16. Mysql> create table messages (
  17. -> Id int (4) unsigned auto_increment primary key,
  18. -> Message varchar (50) not null
  19. ->) Engine = myisam default charset = utf8;
  20. Mysql> insert into messages (message) values ("test MySQL Chinese display ");
  21. Mysql> select * from messages;
  22. + ---- + ------------------- +
  23. | Id | message |
  24. + ---- + ------------------- +
  25. | 1 | test MySQL Chinese display |
  26. + ---- + ------------------- +
  27. Programming (Java)
  28. Import java. SQL. Connection;
  29. Import java. SQL. DriverManager;
  30. Import java. SQL. ResultSet;
  31. Import java. SQL. Statement;
  32. Public class JDBCTest {
  33. Public static void main (String [] args ){
  34. String driver = "com. mysql. jdbc. Driver ";
  35. String url = "jdbc: mysql: // localhost: 3306/test ";
  36. String user = "root ";
  37. String password = "root ";
  38. Try {
  39. Class. forName (driver );
  40. Connection conn = DriverManager. getConnection (url, user, password );
  41. Statement stmt = conn. createStatement ();
  42. Stmt.exe cuteUpdate ("insert into messages (message) values ('test MySQL Code ')");
  43. ResultSet rs = stmt.exe cuteQuery ("select * from messages ");
  44. While (rs. next ()){
  45. Int id = rs. getInt ("id ");
  46. String message = rs. getString ("message ");
  47. System. out. println (id + "" + message );
  48. }
  49. Rs. close ();
  50. Stmt. close ();
  51. Conn. close ();
  52. } Catch (Exception e ){
  53. E. printStackTrace ();
  54. }
  55. }
  56. }
  57. Program output
  58. 1 ???? MySQL ????????
  59. 2 ?? MySQL ??

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.

  1. Mysql> show variables like 'character % ';
  2. + -------------------------- +
  3. | Variable_name | Value |
  4. + -------------------------- +
  5. | Character_set_client | utf8 |
  6. | Character_set_connection | utf8 |
  7. | Character_set_database | utf8 |
  8. | Character_set_filesystem | binary |
  9. | Character_set_results | utf8 |
  10. | Character_set_server | utf8 |
  11. | Character_set_system | utf8 |
  12. | Character_sets_dir | D: \ MySQL \ share \ charsets \ |
  13. + -------------------------- +
  14. Mysql> use test;
  15. Mysql> select * from messages;
  16. + ---- + --------------------------------- +
  17. | Id | message |
  18. + ---- + --------------------------------- +
  19. | 1 | why does one need to modify MySQL statements?
  20. | 2 | ?? MySQL ?? |
  21. + ---- + --------------------------------- +
  22. It is still garbled here mainly because of the different encoding used previously. re-run the previously written Program: java JDBCTest
  23. 1 ???? MySQL ????????
  24. 2 ?? MySQL ??
  25. 3. Test MySQL Encoding
  26. From the third record, we can see that Chinese characters can be properly added and displayed when the program connects to the database.
  27. Mysql> select * from messages;
  28. + ---- + --------------------------------- +
  29. | Id | message |
  30. + ---- + --------------------------------- +
  31. | 1 | why does one need to modify MySQL statements?
  32. | 2 | ?? MySQL ?? |
  33. | 3 | zookeeper MySQL zookeeper |
  34. + ---- + --------------------------------- +

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.

  1. Mysql> show variables like 'character % ';
  2. + -------------------------- +
  3. | Variable_name | Value |
  4. + -------------------------- +
  5. | Character_set_client | gb2312 |
  6. | Character_set_connection | gb2312 |
  7. | Character_set_database | utf8 |
  8. | Character_set_filesystem | binary |
  9. | Character_set_results | gb2312 |
  10. | Character_set_server | utf8 |
  11. | Character_set_system | utf8 |
  12. | Character_sets_dir | D: \ MySQL \ share \ charsets \ |
  13. + -------------------------- +
  14. Mysql> use test;
  15. Mysql> select * from messages;
  16. + ---- + ------------------- +
  17. | Id | message |
  18. + ---- + ------------------- +
  19. | 1 | ???? MySQL ???????? |
  20. | 2 | ?? MySQL ?? |
  21. | 3 | test MySQL encoding |
  22. + ---- + ------------------- +

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 the GUI is used, the program can still be displayed normally after the client encoding is modified (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.

This article permanently updates the link address:

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.