Java,Tomcat,Mysql乱码总结
编码情况:页面编码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乱码情况,这和客户端编码正确与否也有很大关系。