I. Issues
Database code: UTF8
UTF8 COLLATE Utf8_general_ci;
Table code: UTF8
drop table if exists ' test ', create table ' Test ' (' ID ' bigint () not NULL auto_increment COMMENT ' id ', ' name ' varchar (d) Efault ', ' create_time ' timestamp not NULL DEFAULT current_timestamp COMMENT ' creation time ', PRIMARY KEY (' id ')) engine=innodb DEFA ULT charset=UTF8;
JDBC URL:
Url:jdbc:mysql://host:port/dbname
Both the database and database tables already use UTF8 encoding, but they are still garbled when inserting Chinese data.
Two. Causes
when MySQL is connected in JDBC, there is an attribute characterencoding control string encoding in the jdbc URL parameter, which defaults to: AutoDetect. need to be explicitly set to UTF8to resolve the issue.
The MySQL documentation is explained below, see: https://dev.mysql.com/doc/connector-j/5.1/en/ Detailed description of the "Setting Configuration Properties" section in Connector-j-reference-configuration-properties.html.
Three. Workaround
Explicitly set the Characterencoding property to UTF8in the JDBC URL.
url:jdbc:mysql://host:port/dbname?characterencoding=UTF8
Write MySQL Chinese data from memory garbled solution