Java连接MySQL中文乱码处理【转载】
本文转载地址:http://developer.51cto.com/art/200906/130425.htm 感谢相关作者!
MySQL默认编码是latin1
1 mysql> show variables like 'character%'; 2 +--------------------------+--------------------------+ 3 | Variable_name | Value | 4 +--------------------------+--------------------------+ 5 | character_set_client | latin1 | 6 | character_set_connection | latin1 | 7 | character_set_database | latin1 | 8 | character_set_filesystem | binary | 9 | character_set_results | latin1 | 10 | character_set_server | latin1 | 11 | character_set_system | utf8 | 12 | character_sets_dir | D:\MySQL\share\charsets\ | 13 +--------------------------+--------------------------+ 14 创建数据表并插入数据 15 mysql> use test; 16 mysql> create table messages ( 17 -> id int(4) unsigned auto_increment primary key, 18 -> message varchar(50) not null 19 -> ) engine=myisam default charset=utf8; 20 mysql> insert into messages (message) values ("测试MySQL中文显示"); 21 mysql> select * from messages; 22 +----+-------------------+ 23 | id | message | 24 +----+-------------------+ 25 | 1 | 测试MySQL中文显示 | 26 +----+-------------------+ 27 编写程序(Java) 28 import java.sql.Connection; 29 import java.sql.DriverManager; 30 import java.sql.ResultSet; 31 import java.sql.Statement; 32 public class JDBCTest { 33 public static void main(String[] args) { 34 String driver = "com.mysql.jdbc.Driver"; 35 String url = "jdbc:mysql://localhost:3306/test"; 36 String user = "root"; 37 String password = "root"; 38 try { 39 Class.forName(driver); 40 Connection conn = DriverManager.getConnection(url, user, password); 41 Statement stmt = conn.createStatement(); 42 stmt.executeUpdate("insert into messages (message) values ('测试MySQL编码')"); 43 ResultSet rs = stmt.executeQuery("select * from messages"); 44 while (rs.next()) { 45 int id = rs.getInt("id"); 46 String message = rs.getString("message"); 47 System.out.println(id + " " + message); 48 } 49 rs.close(); 50 stmt.close(); 51 conn.close(); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 程序输出 58 1 ????MySQL???????? 59 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
重新连接数据库,查看编码,数据表内容
1 mysql> show variables like 'character%'; 2 +--------------------------+--------------------------+ 3 | Variable_name | Value | 4 +--------------------------+--------------------------+ 5 | character_set_client | utf8 | 6 | character_set_connection | utf8 | 7 | character_set_database | utf8 | 8 | character_set_filesystem | binary | 9 | character_set_results | utf8 | 10 | character_set_server | utf8 | 11 | character_set_system | utf8 | 12 | character_sets_dir | D:\MySQL\share\charsets\ | 13 +--------------------------+--------------------------+ 14 mysql> use test; 15 mysql> select * from messages; 16 +----+-------------------------------+ 17 | id | message | 18 +----+-------------------------------+ 19 | 1 | 虏芒脢脭MySQL脰脨脦脛脧脭脢戮 | 20 | 2 | ??MySQL?? | 21 +----+-------------------------------+ 22 这里显示依然是乱码主要是因为之前使用的编码不同造成的,重新运行之前写的程序:java JDBCTest 23 1 ????MySQL???????? 24 2 ??MySQL?? 25 3 测试MySQL编码 26 从第三条记录我们可以看到现在程序连接数据库时可以正常的添加和显示中文了 27 mysql> select * from messages; 28 +----+-------------------------------+ 29 | id | message | 30 +----+-------------------------------+ 31 | 1 | 虏芒脢脭MySQL脰脨脦脛脧脭脢戮 | 32 | 2 | ??MySQL?? | 33 | 3 | 娴嬭瘯MySQL缂栫爜 | 34 +----+-------------------------------+
看回数据库的显示,我们会很奇怪的发现为什么显示的都是乱码,这主要是和windows下命令行的编码有关,在命令行上查看属性->选项的当前代码页:936 (ANSI/OEM - 简体中文 GBK)(本人机子上是这样显示的)。也就是说命令行上使用的是GBK编码,而我们是在程序连接时使用utf8进行添加的,所以会出现有乱码,现在我们将客户端的编码改成gb2312或gbk试一下
1 mysql> show variables like 'character%'; 2 +--------------------------+--------------------------+ 3 | Variable_name | Value | 4 +--------------------------+--------------------------+ 5 | character_set_client | gb2312 | 6 | character_set_connection | gb2312 | 7 | character_set_database | utf8 | 8 | character_set_filesystem | binary | 9 | character_set_results | gb2312 | 10 | character_set_server | utf8 | 11 | character_set_system | utf8 | 12 | character_sets_dir | D:\MySQL\share\charsets\ | 13 +--------------------------+--------------------------+ 14 mysql> use test; 15 mysql> select * from messages; 16 +----+-------------------+ 17 | id | message | 18 +----+-------------------+ 19 | 1 | ????MySQL???????? | 20 | 2 | ??MySQL?? | 21 | 3 | 测试MySQL编码 | 22 +----+-------------------+
现在可以看到中文正常显示了(主要是因为utf8也支持中文),所以当我们使用命令行工具连接数据库的时候最好将客户端的编码改一下,如果使用GUI的话就不必了,同时修改客户端的编码之后程序依然能够正常显示(以上两点已经测试)
所以如果在程序中要显示中文的话我们可以选用utf8,gb2312,gbk这三种编码,但是如果想在命令行添加中文数据或者查看的话就需要将客户端的编码设置为gb2312或gbk了,还是那句,CMD的编码有关。