时间:2021-07-01 10:21:17 帮助过:16人阅读
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: 3Current database: stevenCurrent user: root@localhostSSL: Not in useUsing delimiter: ;Server version: 5.0.22-community-ntProtocol version: 10Connection: localhost via TCP/IPServer characterset: latin1Db characterset: latin1Client characterset: latin1Conn. characterset: latin1TCP port: 3306Uptime: 2 hours 10 min 15 secThreads: 1 Questions: 121 Slow queries: 0 Opens: 2 Flush tables: 1 Open tables: 0 Queries per second avg: 0.015--------------
mysql> use steven;Database changedmysql> status;--------------mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32)Connection id: 2Current database: stevenCurrent user: root@localhostSSL: Not in useUsing delimiter: ;Server version: 5.0.22-community-ntProtocol version: 10Connection: localhost via TCP/IPServer characterset: gbkDb characterset: latin1Client characterset: gbkConn. characterset: gbkTCP port: 3306Uptime: 1 min 20 secThreads: 1 Questions: 12 Slow queries: 0 Opens: 0 Flush tables: 1 Open tables: 6 Queries per second avg: 0.150--------------发现编码除了Db characterset外其他的都改为gbk了,这是方案一,依然可以实现这样的更改。
C:/Users/Administrator>net stop mysqlThe MySQL service is stopping.The MySQL service was stopped successfully.C:/Users/Administrator>net start mysqlThe MySQL service is starting.The MySQL service was started successfully.这个时候和方案一一样的效果,然而此时并没有解决问题。
mysql> use steven;Database changedmysql> status;--------------mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32)Connection id: 2Current database: stevenCurrent user: root@localhostSSL: Not in useUsing delimiter: ;Server version: 5.0.22-community-ntProtocol version: 10Connection: localhost via TCP/IPServer characterset: gbkDb characterset: latin1Client characterset: gbkConn. characterset: gbkTCP port: 3306Uptime: 1 min 20 secThreads: 1 Questions: 12 Slow queries: 0 Opens: 0 Flush tables: 1 Open tables: 6 Queries per second avg: 0.150--------------
--修改数据库编码为gbk alter database steven character set gbk;其中steven是数据表乱码所在的数据库,这个时候执行status查看
mysql> status;--------------mysql Ver 14.12 Distrib 5.0.22, for Win32 (ia32)Connection id: 2Current database: stevenCurrent user: root@localhostSSL: Not in useUsing delimiter: ;Server version: 5.0.22-community-ntProtocol version: 10Connection: localhost via TCP/IPServer characterset: gbkDb characterset: gbkClient characterset: gbkConn. characterset: gbkTCP port: 3306Uptime: 8 min 30 secThreads: 1 Questions: 32 Slow queries: 0 Opens: 1 Flush tables: 1 Open tables: 7 Queries per second avg: 0.063--------------此时都成为gbk的编码格式了。 注:如果此时数据库编码默认就为gbk的编码方式的,此时问题一般都已经解决了,如果还没有解决,在插入含有中文数据的时候会有以下异常:
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自带的命令执行以下两句命令:
--修改表默认用gbkalter table t_user character set gbk;--修改userName字段编码为gbkalter table t_user modify userName varchar(100) CHARACTER SET gbk;执行完之后,数据库字段和表的编码方式都更改为了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; --修改表默认用gbkalter table t_user(表的名称) character set gbk;--修改userName字段编码为utf8alter 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;bitsCN.com