当前位置:Gxlcms > mysql > Mysql中文乱码解决思路和过程_MySQL

Mysql中文乱码解决思路和过程_MySQL

时间:2021-07-01 10:21:17 帮助过:16人阅读

bitsCN.com

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)

解决思路

在javaweb项目中,在解决乱码问题之前,首先要确保是在数据库这里发生乱码,如果确实在数据库这端发生乱码,请试着通过以下步骤查找原因:
步骤一:查看数据库编码状态,如下所示:
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--------------

发现Server,Db,Client,Conn.的字符编码均为latin1,所以一定会出现中文乱码的情况。 解决方案一:(window下) 通过MySQL Server Instance Configuration Wizard重新设置编码方案,如图2所示: /
图2 然后一直Next,然后到Please select the database usage 的时候选择第三项,如图3所示: /
图3 接着一直Next,到Please select the default character set.的时候选择gbk,字符编码,如图4所示: /
图4 这个时候一直Next,确认密码后,Next,然后就Execute执行,如图5所示: /
图5 Finish后,退出数据库命令控制台然后再进去,查看数据库编码状态,如下所示:
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了,这是方案一,依然可以实现这样的更改。
解决方案二:通过配置文件,找到Mysql安装的目录,找到根目录下my.ini文件,如图6所示: /
图6 然后打开后,进行如下更改,如图7所示: /
图7 将latin1更改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--------------

步骤二:改变Db characterset的编码方式,执行以下sql语句:
 --修改数据库编码为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,所以异常的原因就是中文的编码和表的默认编码不匹配,造成数据冲突引起的。
步骤三:解决异常执行以下两句进行更改数据库的默认编码方式; 方案一:drop表,然后重新建表,建表语句如下所示;
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数据库编码已经解决了。

总结:对于数据库乱码解决方案,可以归纳如下: 通过软件或者配置文件进行第一步修改编码通过mysql命令更改数据库编码(如果默认编码支持中文,此步骤就可以省略了)发生Data too long for column……异常,进行数据表字符编码更改 一般情况的mysql数据库中文编码问题即可得到解决,编码同样可以设置成utf8编码方式,但有时会出现数据库中编码是繁体字的现象,然后执行set names "gbk"命令,即可成功显示简体中文。
下面提供常用的修改中文字符乱码的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

人气教程排行