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".
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;
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;
jdbc url:
url: jdbc:mysql://host:port/dbname
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
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!