Detailed explanation of the solution to garbled characters when JDBC connects to MySQL to process Chinese

Detailed explanation of the solution to garbled characters when JDBC connects to MySQL to process Chinese

Detailed explanation of the solution to garbled characters when JDBC connects to MySQL to process Chinese

Recently, the integrated project needs to connect to an older version of MySQL server. Using Navicat to check, it is found that this MySQL server does not seem to have a default encoding set, and from some PHP files that operate this MySQL, it should be using the gb2312 encoding. However, when using JDBC operations directly, the Chinese characters read from the database are all garbled.

At first, I tried to force encoding conversion using methods like entity.setDepartName(new String(rs.getString("hg").getBytes("gbk"), "utf-8"));, but it failed because no matter which method was used, the converted characters were always garbled, but the way they were garbled was different each time. Quite depressed. Moreover, since this project uses other products, it is not possible to add additional things like filters, so this problem is not easy to deal with.

There was no problem using navicat to connect and query, so I tried to export a table to sql to see if there was any encoding setting in the DDL. The result disappointed me because the encoding was not written at all. So, I changed the extension of the exported sql file to html, opened it with IE, and found that there was no garbled code. I checked the encoding format and found that it was "gb2312". However, using java to force transcoding was of no avail. What should I do?

Moreover, this project has been running for many years, and there is a lack of post-maintenance. The my.ini file cannot be viewed and modified.

Suddenly I remembered that I can add parameters when connecting to MySQL, and some parameters specify encoding. Can this solve the problem?

So modify the connection string (the original value is: 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 and check, OK! Chinese is normal.

Problem solved.

This method actually specifies the gbk encoding format when connecting, thereby avoiding the client and server using their own default encoding formats to interact. As long as the configuration is appropriate, there will be no garbled code problem.

If you have any questions, please leave a message or come to the community to discuss. Thank you for reading and I hope it can help you. Thank you for your support of this site!

You may also be interested in:
  • Example solution for writing garbled Chinese characters into MySQL in PHP
  • MySQL character set garbled characters and solutions
  • Solution to MySQL garbled code problem under Linux
  • How to solve the problem of Chinese garbled characters when inserting table data into MySQL
  • Summary of handling JDBC connection mysql garbled code exception problem
  • Detailed explanation of the Chinese garbled characters problem in MySQL database
  • Solve the problem of garbled data in MySQL database migration

<<:  VMware Tools installation and configuration graphic tutorial for Ubuntu 16.04 64-bit

>>:  Detailed explanation of React setState data update mechanism

Recommend

Detailed process of building mysql5.7.29 on centos7 of linux

1. Download MySQL 1.1 Download address https://do...

Detailed explanation of React setState data update mechanism

Table of contents Why use setState Usage of setSt...

Analysis of three parameters of MySQL replication problem

Table of contents 01 sql_slave_skip_counter param...

Summary of common commands for Linux user and group management

This article summarizes the common commands for L...

html page!--[if IE]...![endif]--Detailed introduction to usage

Copy code The code is as follows: <!--[if IE]&...

Docker image loading principle

Table of contents Docker images What is a mirror?...

Steps to customize icon in Vue

ant-design-vue customizes the use of Ali iconfont...

Text mode in IE! Introduction to the role of DOCTYPE

After solving the form auto-fill problem discussed...

The homepage design best reflects the level of the web designer

In the many projects I have worked on, there is b...

Detailed explanation of the API in Vue.js that is easy to overlook

Table of contents nextTick v-model syntax sugar ....

How to embed flash video format (flv, swf) files in html files

Flash file formats: .FLV and .SWF There are two ex...

Using vsftp to build an FTP server under Linux (with parameter description)

introduce This chapter mainly introduces the proc...

Steps to build MHA architecture deployment in MySQL

Table of contents MAH 1. Introduction to MAH Arch...

Problems and solutions when installing MySQL8.0.13 on Win10 system

Operating system: Window10 MySQL version: 8.0.13-...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...