Detailed solution for garbled characters when connecting JDBC to mysql to process Chinese characters, jdbcmysql

Source: Internet
Author: User

Detailed solution for garbled characters when connecting JDBC to mysql to process Chinese characters, jdbcmysql

How to troubleshoot Chinese garbled characters when connecting to mysql through JDBC

Recently, the integrated project needs to be connected to a mysql Server of an earlier version. You can use navicat to check whether the default encoding is set for this mysql server. In addition, you can see that some PHP files are operated on this mysql server, it should be the gb2312 encoding. However, the jdbc operation is used directly, and all Chinese characters read from the database are garbled.

At the beginning, using entity. setcommonname (new String (rs. getString ("hg "). getBytes ("gbk"), "UTF-8"); method, trying to force the encoding conversion, the result fails, because, regardless of the method, the characters transferred out are always garbled characters, but they are different in different ways. Relatively depressing. In addition, this project is not able to add other similar filters to other products, so this problem is not very well handled.

There is no problem with using navicat to connect to the query. Therefore, I tried to export an SQL statement to a table and check whether there are any encoding settings in the DDL statement. The result was disappointing, and the encoding was not directly written. Therefore, we changed the exported SQL file extension to html and opened it with IE. No garbled code was found. Check that the encoding format is "gb2312". However, using java to force transcoding does not help. What should we do?

In addition, this project has been running for many years, and there is a lack of maintenance in the future, and the my. ini file cannot be viewed and modified.

Suddenly, when mysql is connected, parameters can be added and some parameters are encoded. Can this solve the problem?

Therefore, modify the connection string (original value: url = "jdbc: mysql: // 192.168.18.254: 3306/web_oa):

url="jdbc:mysql://192.168.18.254:3306/web_oa?useUnicode=true&characterEncoding=gbk"

Restart the application and check it. OK! Chinese is normal.

Solve the problem.

This method is used to specify the gbk encoding format during connection, so as to avoid interaction between the client and the server using their own default encoding format, as long as the configuration is appropriate, there will be no garbled problem.

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.