Home > Database > Mysql Tutorial > body text

How to solve the Chinese garbled problem of mysql jdbc

藏色散人
Release: 2021-12-21 15:54:17
Original
4077 people have browsed it

Mysql The solution to jdbc Chinese garbled characters is to explicitly set the characterEncoding attribute to utf8 in the jdbc url, with a code such as "jdbc:mysql://host:port/dbname?characterEncoding=utf8".

How to solve the Chinese garbled problem of mysql jdbc

The operating environment of this article: Windows 7 system, Mysql version 5.7, Dell G3 computer.

How to solve the Chinese garbled code problem in mysql jdbc?

jdbc mysql writing Chinese garbled code solution

1. Question

Database encoding: utf8

mysql> create database dbnameDEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Copy after login

Table encoding: utf8

drop table if exists `test`;
create table `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(50) default '',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

jdbc url:

url: jdbc:mysql://host:port/dbname
Copy after login

Both the database and the database table have used utf8 encoding, but when inserting Chinese data, it is still garbled.

2. Reason

When connecting to mysql in jdbc, there is an attribute characterEncoding in the jdbc url parameter to control the string encoding. This value defaults to For: autodetect. needs to be explicitly set to utf8, which can solve the problem.

MySQL documentation explains it as follows, for details, see: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties. Detailed description in the "Setting Configuration Properties" section of html.

3. Solution

In jdbc url Explicitly set the characterEncoding attribute to utf8.

url: jdbc:mysql://host:port/dbname?characterEncoding=utf8
Copy after login

Recommended learning: "mysql video tutorial"

The above is the detailed content of How to solve the Chinese garbled problem of mysql jdbc. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template