JDBC connection mysql processing Chinese garbled code solution details

  • 2021-09-16 08:27:42
  • OfStack

Detailed explanation of the solution of garbled code when JDBC connects mysql to deal with Chinese

Recently, the integrated project needs to be connected with an older version of mysql server. Looking at it with navicat, it is found that this mysql server does not seem to have a default code, and from the part of php files that operate this mysql, it should be the code of gb2312. However, using jdbc operation directly, all the Chinese read from the library are garbled.

1, use similar entity. setDepartName (new String (rs. getString ("hg"). getBytes ("gbk"), "utf-8")); Attempt to force the encoding conversion, the result failed, because, no matter which way, the transferred characters are always a variety of random code, but each random way is not the same. It's depressing. Moreover, because of other products used in this project, it is impossible to add additional things like filters, so this problem is not very easy to deal with.

There is no problem using navicat join query, so try to export a table to sql and see if there is any encoding setting in DDL. The result makes me disappointed, and the encoding block is not written directly. Therefore, the extension of the exported sql file was changed to html, and it was opened with IE, and it was found that there was no garbled code. Looking at the encoding format at this time, it was really "gb2312", but it was useless to force transcoding with java. What shall I do?

Moreover, this project has been running for many years, and the later maintenance is somewhat lacking, and the my. ini file cannot be viewed and modified.

It suddenly occurred to me that mysql connection can be added parameters, and some parameters are specified encoded, can this solve the problem?

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


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

Restart the application, view, OK! Chinese is normal.

Problem solving.

In fact, this method specifies the encoding format of gbk when connecting, so as to avoid the 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 code problem.

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: