您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何終結mysql中文亂碼,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
我自己都不知道為了中文問題煩惱了多少個日子,最恨瑞典人了-誰叫他發明了mysql,還設置了默認設置:latin1.曾經也在想假如中國人發明了 mysql(my see狗),呵呵,那么我們還有這樣煩惱嗎?默認的就是gk2312 或者gbk.倘若考慮到臺灣朋友加一個big5.就得了。可是呢?事實不是這樣的,沒有辦法,自己只好baidu一下,google一下,甚至yahoo 一下。能找到的方法都試過了,好久了,直到今天讓我找到了,想明白了。所以急切和大家分享以下心得。
為了說的明白一些,我覺個例子:
很簡單,就是從html中接受兩個輸入,然后由jsp處理寫到mysql且從數據庫返回這個結果顯示出來。
Mysql_jstl.html
|
接下來就是創建數據庫,名字為sample
然后建立一個table: employee,內容如下(其中應該是email,可是我不小心在建數據庫打錯了,將錯就錯了):
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| employee_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| last_name | varchar(20) | YES | | NULL | |
| first_name | varchar(20) | YES | | NULL | |
| birth | date | YES | | NULL | |
| sex | enum('m','f') | YES | | m | |
| emmail | varchar(39) | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
如果只是這樣的話,就會出現這樣的錯誤:報告的錯誤是:sqle=com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long
for column 'last_name' at row 1 從 employee!
有的情況是:| name |
+-------------+
| ?? |
| 54243654321 |
| ?? |
| ?? |
+-------------+出現問號!
前面我們已經說過了,mysql默認的編碼是latin1,不是我們所需要的gbk,所以我們要修改成為utf8,因為若要正確顯示中文繁、簡、日文、韓文 使用utf8,修改方法如下:
ALTER DATABASE sample ####這里修改整個數據庫的編碼
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;
當然了,你也可在在建數據庫的時候指定編碼,比如:
CREATE DATABASE sample
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;
接下來要做的是打開mysql所在的目錄下的my.nin
mysql> show variables like '%character%'; +--------------------------+-------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_results | latin1 | | character_set_server | utf8 | character_set_system | utf8 | | character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets | +--------------------------+-------------------------------------------+ mysql> show variables like '%collation%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | latin1_swedish_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-------------------+
接著你再看看執行那個Mysql.html 文件:這回你可以看到的是
mysql> select * from employee;
+-------------+-----------+------------+------------+------+--------------+
| employee_id | last_name | first_name | birth | sex | emmail |
+-------------+-----------+------------+------------+------+--------------+
| 12 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 13 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 14 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 15 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 16 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
| 17 | ? | ?? | 1978-12-11 | f | aaa@asdf.com |
+-------------+-----------+------------+------------+------+--------------+
這當然不是我們希望看到的,我們需要的現實完美正確的中文:
我們還有最后一招:
mysql> SET NAMES 'gbk' ;Query OK, 0 rows affected (0.00 sec)
因為我們需要的是gbk.
看看mysql中的character設置情況:
mysql> SHOW VARIABLES LIKE '%character%' ;
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets |
+--------------------------+-------------------------------------------+
7 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE '%collation%' ;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
這個才是我們最終需要的。
在來查看執行完Mysql_jstl.jsp后的數據庫中的結果:
mysql> select * from employee;
| 14 | 王 | 彭給 | 1978-12-11 | f | aaa@asdf.com |
| 15 | 田 | 王光 | 1978-12-11 | f | aaa@asdf.com |
| 16 | 息 | 存入 | 1978-12-11 | f | aaa@asdf.com |
| 17 | 往 | 小杯 | 1978-12-11 | f | aaa@asdf.com |
+-------------+-----------+------------+------------+------+--------------+
可以高興得看到了中文,并且在瀏覽器中也顯示正確。
但是僅僅這樣的話,當你重新啟動mysql的時候
所有的設置又失效了。
mysql> show variables like '%character%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | C:MySQLMySQL Server 5.0sharecharsets |
+--------------------------+-------------------------------------------+
mysql> SHOW VARIABLES LIKE '%collation%' ;
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-------------------+
重新讀取又出現亂碼:
mysql> select * from employee;
+-------------+-----------+------------+------------+--
| employee_id | last_name | first_name | birth | s
+-------------+-----------+------------+------------+--
| 12 | ? | ?? | 1978-12-11 | f
| 13 | ? | ?? | 1978-12-11 | f
| 14 | ? | ?? | 1978-12-11 | f
| 15 | ? | ?? | 1978-12-11 | f
| 16 | ? | ?? | 1978-12-11 | f
| 17 | ? | ?? | 1978-12-11 | f
+-------------+-----------+------------+------------+--
所以我們需要在客戶端設置系統能識別中文的編碼gbk并沒有保存到my.ini文件中。所以要修改my.ini文件
在[mysql]段加入一下代碼改成:default-character-set=gbk 這樣設置就得到保存了。
重啟就可以了。mysql> show variables like '%character%';
+--------------------------+----------------
| Variable_name | Value
+--------------------------+----------------
| character_set_client | gbk
| character_set_connection | gbk
| character_set_database | utf8
| character_set_results | gbk
| character_set_server | utf8
| character_set_system | utf8
| character_sets_dir | C:MySQLMySQL
+--------------------------+----------------
mysql> SHOW VARIABLES LIKE '%collation%' ;
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | gbk_chinese_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
以上就是如何終結mysql中文亂碼,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。