解决乱码最好的方法是在项目设计之初,统一所有的字符集,例如页面、request对象以及数据库等等。
一、 几种常见的乱码现象:
1、页面乱码
单纯的页面乱码是很好解决的,只要修改头部适合的字符集即可,如果页面中文显示乱码,你可以把字符集修改为:gb2312或gbk。
2、页面之间传递的参数是乱码
页面之间传递参数出现的乱码,就要解决页面编码和requset的字符集问题,在参数传进传出时重新设置相应的字符集。比如:request.setCharacterEncoding("UTF-8");
3、数据库乱码(下面详细介绍)
二、数据库乱码解决方法(Mysql)
页面和页面传值都没有问题,结果发现存储在数据库中的数据出现了乱码,则问题是出现在数据库的编码问题。
下面我已MySql数据库为例说明解决方法:
如果你安装数据库的时候字符集使用的默认选项:latin1
DOS命令下进入数据库show 一下字符集(命令:show variables like"%char%"; )显示如下:
- +--------------------------+---------------+
- | 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 |
- +--------------------------+---------------+
+--------------------------+---------------+
| 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 |
+--------------------------+---------------+
1、修改数据库默认字符集:
打开C:\Program Files\MySQL\MySQL Server 5.0里面的my.ini文件找到这段文字:
引用
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=latin1
把default-character-set=latin1改为default-character-set=gbk
这时候show 一下字符集(重新启动mysql服务 ,命令:show variables like"%char%"; )显示如下:
- +--------------------------+---------------+
- | Variable_name | Value |
- +--------------------------+---------------+
- | character_set_client | latin1 |
- | character_set_connection | latin1 |
- | character_set_database | gbk |
- | character_set_filesystem | binary |
- | character_set_results | latin1 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- +--------------------------+---------------+
+--------------------------+---------------+
| Variable_name | Value |
+--------------------------+---------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+---------------+
2、如果你是导入的数据库,需要对my.ini做如下修改找到
引用
[client]
port=3306
[mysql]
default-character-set=latin1
把 default-character-set=latin1改为default-character-set=gbk
引用
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
找到
增加default-character-set=gbk
然后在设置一下你导入的数据库字符集即可:
命令:alter database 你的数据库名 charset = gbk;
重新启动mysql服务,show 一下字符集(,命令:show variables like"%char%"; )显示如下:
- +--------------------------+---------------+
- | Variable_name | Value |
- +--------------------------+---------------+
- | character_set_client | gbk |
- | character_set_connection | gbk |
- | character_set_database | gbk |
- | character_set_filesystem | binary |
- | character_set_results | gbk |
- | character_set_server | gbk |
- | character_set_system | utf8 |
- +--------------------------+---------------+
+--------------------------+---------------+
| Variable_name | Value |
+--------------------------+---------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | gbk |
| character_set_system | utf8 |
+--------------------------+---------------+
如果你见到如上内容,数据库字符集问题已解决。
如果你的项目还有字符集问题,那就需要查看一下是否是其他因素导致的乱码了,比如我开篇时提到的问题。