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编码 | +----+-------------------+
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11