使用Hibernate向MySQL數據庫里寫了數據,全是亂碼:
mysql> select * from tbl_Category;
+----+--------------------+--------------------+----------+
| id | descr | name | parentId |
+----+--------------------+--------------------+----------+
| 1 | ???????????? | ???????????? | NULL |
| 2 | ???????????? | ???????????? | NULL |
| 3 | ???????????? | ???????????? | NULL |
| 4 | ???????????? | ???????????? | NULL |
| 5 | ???????????? | ???????????? | NULL |
| 6 | ?????????????????? | ?????????????????? | NULL |
+----+--------------------+--------------------+----------+
6 rows in set (0.10 sec)
為了能處理Unicode編碼,最好將數據庫庫的編碼設置為UTF8(這樣就可以不管是哪國文字都能正常顯示),連接時也用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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
以上是未設置前的編碼情況,通過以下命令來設置編碼:
mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
使用set命令將相應的參數設置相應的值,請自己一個一個設置,下面是設置好了的。
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
到此為止,MySQL數據庫編碼被我們設置好了。
在連接MySQL數據庫時,使用如下連接串:
jdbc.url=jdbc:mysql://192.168.1.114:3306/szblcz?useUnicode=true&characterEncoding=UTF-8
我這里是配置在一個Properties文件里。useUnicode=true&characterEncoding=UTF-8 這兩個參數起到了至關重要的作用。
到這里一切再測試一下,應該沒問題了。
mysql> select * from tbl_Category;
+----+--------------------+--------------------+----------+
| id | descr | name | parentId |
+----+--------------------+--------------------+----------+
| 1 | ???????????? | ???????????? | NULL |
| 2 | ???????????? | ???????????? | NULL |
| 3 | ???????????? | ???????????? | NULL |
| 4 | ???????????? | ???????????? | NULL |
| 5 | ???????????? | ???????????? | NULL |
| 6 | ?????????????????? | ?????????????????? | NULL |
+----+--------------------+--------------------+----------+
6 rows in set (0.10 sec)為了能處理Unicode編碼,最好將數據庫庫的編碼設置為UTF8(這樣就可以不管是哪國文字都能正常顯示),連接時也用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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)在連接MySQL數據庫時,使用如下連接串:
jdbc.url=jdbc:mysql://192.168.1.114:3306/szblcz?useUnicode=true&characterEncoding=UTF-8到這里一切再測試一下,應該沒問題了。

