解決亂碼最好的方法是在項目設計之初,統一所有的字符集,例如頁面、request對象以及數據庫等等。
一、 幾種常見的亂碼現象:
1、頁面亂碼
單純的頁面亂碼是很好解決的,只要修改頭部適合的字符集即可,如果頁面中文顯示亂碼,你可以把字符集修改為:gb2312或gbk。
2、頁面之間傳遞的參數是亂碼
頁面之間傳遞參數出現的亂碼,就要解決頁面編碼和requset的字符集問題,在參數傳進傳出時重新設置相應的字符集。比如:request.setCharacterEncoding("UTF-8");
3、數據庫亂碼(下面詳細介紹)
二、數據庫亂碼解決方法(MySQL)
頁面和頁面傳值都沒有問題,結果發現存儲在數據庫中的數據出現了亂碼,則問題是出現在數據庫的編碼問題。
下面我已MySQL數據庫為例說明解決方法:
如果你安裝數據庫的時候字符集使用的默認選項:latin1
DOS命令下進入數據庫show 一下字符集(命令:show variables like"%char%"; )顯示如下:
Java代碼
- +--------------------------+---------------+
- | 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%"; )顯示如下:
Java代碼
- +--------------------------+---------------+
- | 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%"; )顯示如下:
Java代碼
- +--------------------------+---------------+
- | 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 |
+--------------------------+---------------+
如果你見到如上內容,數據庫字符集問題已解決。
如果你的項目還有字符集問題,那就需要查看一下是否是其他因素導致的亂碼了,比如我開篇時提到的問題