編碼情況:頁面編碼gbk2312,java編gbk,mysql database table 編碼utf-8 ,tomcat預設編碼=gbk
亂碼出現情況:頁面輸入中文,insert的時候,在tomcat層級顯示正常,資料庫顯示亂碼;
若直接在資料庫中insert,在頁面顯示卻正常
錯誤出現原因:mysql的server編碼沒有修改。
解決辦法: mysql.ini設定檔 default_character_set=utf8
知識介紹:
MySQL 4.1的字元集支援(Character Set Support)有兩個方面:字元集(Character set)和排序方式(Collation)。對於字元集的支援細化到四個層次: 伺服器(server),資料庫(database),資料表(table)和串連(connection)。
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| 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 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
通過修改mysql.ini設定檔 default_character_set=utf8後
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8|
| character_set_filesystem | binary|
| character_set_results | latin1 |
| character_set_server | utf8|
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)
其實亂碼出現就幾個層次:頁面編碼控制了顯示和輸出,IDE的代碼編碼控制了編譯,tomcat URIEncoding控制了伺服器的編碼,串連方式控制了server端串連訪問mysql編碼,mysql控制層次是如上提到的:表,資料庫,串連,伺服器,其中mysql的預設伺服器utf-8,其餘都是預設編譯時間的編碼。
最方便和不出問題的解決辦法,就是所有配置均使用統一編碼。當然這也不是萬無一失的,筆者就碰見過在手機瀏覽器上input亂碼情況,這和用戶端編碼正確與否也有很大關係。