Mysql 中文乱码问题是常见的问题,解决也是不难的,本文将通过我在遇到问题时候,如何的查找和解决为导向,来呈现分析解决问题的思路和过程。
在JavaEE项目开发中,通常的中文乱码问题分为web前端,web后端,数据库乱码,如果想查看web端的中文乱码问题,请参照博文http://blog.csdn.net/songdeitao/article/details/17577823,一但排除了web端的中文乱码,也就说乱码问题是在数据库端发生的了。
首先在创建用户,如图1所示:
图1
在点击增加用户的时候,数据库中userName字段显示为乱码:
mysql> select * from t_user; +--------+----------+------+---------------------+-------+ | userId | userName | age | birthday | isVip | +--------+----------+------+---------------------+-------+ | 1 | ???? | 0 | 2014-01-01 00:00:00 | 1 | +--------+----------+------+---------------------+-------+ 1 row in set (0.00 sec)
mysql> status; -------------- mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32) Connection id: 3 Current database: steven Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.0.22-community-nt Protocol version: 10 Connection: localhost via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 TCP port: 3306 Uptime: 2 hours 10 min 15 sec Threads: 1 Questions: 121 Slow queries: 0 Opens: 2 Flush tables: 1 Open tab les: 0 Queries per second avg: 0.015 --------------
mysql> use steven; Database changed mysql> status; -------------- mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32) Connection id: 2 Current database: steven Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.0.22-community-nt Protocol version: 10 Connection: localhost via TCP/IP Server characterset: gbk Db characterset: latin1 Client characterset: gbk Conn. characterset: gbk TCP port: 3306 Uptime: 1 min 20 sec Threads: 1 Questions: 12 Slow queries: 0 Opens: 0 Flush tables: 1 Open tabl es: 6 Queries per second avg: 0.150 --------------
C:\Users\Administrator>net stop mysql The MySQL service is stopping. The MySQL service was stopped successfully. C:\Users\Administrator>net start mysql The MySQL service is starting. The MySQL service was started successfully.这个时候和方案一一样的效果,然而此时并没有解决问题。
mysql> use steven; Database changed mysql> status; -------------- mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32) Connection id: 2 Current database: steven Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.0.22-community-nt Protocol version: 10 Connection: localhost via TCP/IP Server characterset: gbk Db characterset: latin1 Client characterset: gbk Conn. characterset: gbk TCP port: 3306 Uptime: 1 min 20 sec Threads: 1 Questions: 12 Slow queries: 0 Opens: 0 Flush tables: 1 Open tabl es: 6 Queries per second avg: 0.150 --------------
--修改数据库编码为gbk alter database steven character set gbk;
mysql> status; -------------- mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32) Connection id: 2 Current database: steven Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.0.22-community-nt Protocol version: 10 Connection: localhost via TCP/IP Server characterset: gbk Db characterset: gbk Client characterset: gbk Conn. characterset: gbk TCP port: 3306 Uptime: 8 min 30 sec Threads: 1 Questions: 32 Slow queries: 0 Opens: 1 Flush tables: 1 Open tabl es: 7 Queries per second avg: 0.063 --------------
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'userName' at row 1 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922) at com.steven.util.DaoHandle.executeDML(DaoHandle.java:49) at com.steven.dao.impl.UserDao.doCreate(UserDao.java:33) at com.steven.model.UserAddAction.execute(UserAddAction.java:80) at com.steven.controller.ActionServlet.doPost(ActionServlet.java:40) at javax.servlet.http.HttpServlet.service(HttpServlet.java:643) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.steven.util.EncodeFilter.doFilter(EncodeFilter.java:35) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:722)
mysql> show create table t_user; +--------+---------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ----------------------------------------+ | Table | Create Table | +--------+---------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ----------------------------------------+ | t_user | CREATE TABLE `t_user` ( `userId` int(10) NOT NULL auto_increment, `userName` varchar(100) NOT NULL, `age` int(2) default NULL, `birthday` datetime default NULL, `isVip` tinyint(1) default NULL, PRIMARY KEY (`userId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +--------+---------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- ----------------------------------------+ 1 row in set (0.00 sec)会发现数据表默认编码是latin1,所以异常的原因就是中文的编码和表的默认编码不匹配,造成数据冲突引起的。
create table t_user( userId int(10) not null primary key auto_increment, userName varchar(100) not null, age int(2), birthday datetime, isVip boolean ) DEFAULT CHARSET=gbk;方案二:mysql自带的命令
--修改表默认用gbk alter table t_user character set gbk; --修改userName字段编码为gbk alter table t_user modify userName varchar(100) CHARACTER SET gbk;
mysql> select * from t_user; +--------+----------+------+---------------------+-------+ | userId | userName | age | birthday | isVip | +--------+----------+------+---------------------+-------+ | 1 | ???? | 0 | 2014-01-01 00:00:00 | 1 | | 2 | 元旦快乐 | 0 | 2014-01-01 00:00:00 | 1 | +--------+----------+------+---------------------+-------+ 2 rows in set (0.00 sec)第一条为乱码时候插入的数据,第二条为解决后插入的数据,此时Mysql数据库编码已经解决了。
set names 'gbk'; --它相当于下面的三句指令: set character_set_client = gbk; set character_set_results = gbk; set character_set_connection = gbk; --显示数据表表的编码 show create table t_user(表的名称); --修改数据库编码为gbk alter database steven(数据库的名称) character set gbk; --修改表默认用gbk alter table t_user(表的名称) character set gbk; --修改userName字段编码为utf8 alter table t_user(表的名称) modify userName(表中字段的名称) varchar(100) CHARACTER SET gbk; --创建表 create table t_user( userId int(10) not null primary key auto_increment, userName varchar(100) not null, age int(2), birthday datetime, isVip boolean ) DEFAULT CHARSET=gbk;