Java,Tomcat,Mysql亂碼總結

來源:互聯網
上載者:User

  編碼情況:頁面編碼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亂碼情況,這和用戶端編碼正確與否也有很大關係。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.