java向mysql插入数据乱码问题解决
MySQL默认编码是latin1
mysql> show variables like 'character%'; +--------------------------+--------------------------+ | Variable_name | Value | +--------------------------+--------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | D:\MySQL\share\charsets\ | +--------------------------+--------------------------+ 创建数据表并插入数据 mysql> use test; mysql> create table messages ( -> id int(4) unsigned auto_increment primary key, -> message varchar(50) not null -> ) engine=myisam default charset=utf8; mysql> insert into messages (message) values ("测试MySQL中文显示"); mysql> select * from messages; +----+-------------------+ | id | message | +----+-------------------+ | 1 | 测试MySQL中文显示 | +----+-------------------+
编写程序(Java)
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class JDBCTest { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "root"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); stmt.executeUpdate("insert into messages (message) values ('测试MySQL编码')"); ResultSet rs = stmt.executeQuery("select * from messages"); while (rs.next()) { int id = rs.getInt("id"); String message = rs.getString("message"); System.out.println(id + " " + message); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } 程序输出 1 ????MySQL???????? 2 ??MySQL??
我们看到,尽管使用数据库时我们能够正常的添加和显示中文,但是在使用程序连接数据库时并不能够正常显示中文,为此我们需要修改MySQL的默认编码,编辑my.ini(MySQL配置文件)文件对编码进行修改
设置MySQL的默认字符集为utf8,找到客户端配置[client]在下面添加。
default-character-set=utf8
找到服务器配置[mysqld]在下面添加
default-character-set=utf8
设定MySQL数据库以utf8编码运行,连接MySQL数据库时使用utf8编码
停止和重新启动MySQL
net stop mysql
net start mysql
重新连接数据库,查看编码,数据表内容
mysql> show variables like 'character%'; +--------------------------+--------------------------+ | Variable_name | Value | +--------------------------+--------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | D:\MySQL\share\charsets\ | +--------------------------+--------------------------+ mysql> use test; mysql> select * from messages; +----+-------------------------------+ | id | message | +----+-------------------------------+ | 1 | 虏芒脢脭MySQL脰脨脦脛脧脭脢戮 | | 2 | ??MySQL?? | +----+-------------------------------+ 这里显示依然是乱码主要是因为之前使用的编码不同造成的,重新运行之前写的程序:java JDBCTest 1 ????MySQL???????? 2 ??MySQL?? 3 测试MySQL编码 从第三条记录我们可以看到现在程序连接数据库时可以正常的添加和显示中文了 mysql> select * from messages; +----+-------------------------------+ | id | message | +----+-------------------------------+ | 1 | 虏芒脢脭MySQL脰脨脦脛脧脭脢戮 | | 2 | ??MySQL?? | | 3 | 娴嬭瘯MySQL缂栫爜 | +----+-------------------------------+
看回数据库的显示,我们会很奇怪的发现为什么显示的都是乱码,这主要是和windows下命令行的编码有关,在命令行上查看属性->选项的当前代码页:936 (ANSI/OEM - 简体中文 GBK)(本人机子上是这样显示的)
也就是说命令行上使用的是GBK编码,而我们是在程序连接时使用utf8进行添加的,所以会出现有乱码,现在我们将客户端的编码改成gb2312或gbk试一下
mysql> show variables like 'character%'; +--------------------------+--------------------------+ | Variable_name | Value | +--------------------------+--------------------------+ | character_set_client | gb2312 | | character_set_connection | gb2312 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | gb2312 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | D:\MySQL\share\charsets\ | +--------------------------+--------------------------+ mysql> use test; mysql> select * from messages; +----+-------------------+ | id | message | +----+-------------------+ | 1 | ????MySQL???????? | | 2 | ??MySQL?? | | 3 | 测试MySQL编码 | +----+-------------------+
现在可以看到中文正常显示了(主要是因为utf8也支持中文),所以当我们使用命令行工具连接数据库的时候最好将客户端的编码改一下,如果使用GUI的话就不必了,同时修改客户端的编码之后程序依然能够正常显示(以上两点已经测试)
所以如果在程序中要显示中文的话我们可以选用utf8,gb2312,gbk这三种编码,但是如果想在命令行添加中文数据或者查看的话就需要将客户端的编码设置为gb2312或gbk了,还是那句,CMD的编码有关