解決亂碼最好的方法是在項目設計之初,統一所有的字符集,例如頁面、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 | +--------------------------+---------------+
如果你見到如上內容,數據庫字符集問題已解決。
如果你的項目還有字符集問題,那就需要查看一下是否是其他因素導致的亂碼了,比如我開篇時提到的問題。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
