日期:2014-05-16 浏览次数:20611 次
搭建了一个Hibernate+Spring+ZKjsp的框架,前几天没有太注意,一直用输英文的状态测试,前天发现中文输入全是“?”号,今天问题在sun4love的提醒下终于解决了问题,所以总结一下,以便大家遇到相同的问题的时候能够得到帮助。
没有想明白的问题是:使用JUnit测试中文能够保存,而使用tomcat容器就会出现中文乱码?Junit测试和tomcat容器使用的是同一个配置文件,链接数据库的代码:
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/logisticsect?useUnicode=true&characterEncoding=UTF-8 jdbc.username = root jdbc.password = 111111
?对了,tomcat还配置了/conf/server.xml:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
??按照配置应该是没有问题的,可是为什么就是存不入中文呢,郁闷,纠结.....
问题产生原因,数据库的字符集编码不一致;建议修改为utf-8;
设置前:
修改后:
?
解决问题办法:
1.通过重新配置服务修改(很简单):
启动:开始-MySQL-MySQL Server 5.1-MySQL Server Instance config Wizard.
2.通过MySQL命令行修改:
#创建数据库指定数据库的字符集(注意set utf8,而不是utf-8)
mysql>create database mydb character set utf8;?#修改数据库的字符集
mysql>use mydb mysql>alter database mydb character set utf8;?#设置数据库字符集?#查看字符集
mysql> set character_set_client=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_connection=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_database=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_results=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_server=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_system=utf8; Query OK, 0 rows affected (0.01 sec) mysql> set collation_connection=utf8; Query OK, 0 rows affected (0.01 sec) mysql> set collation_database=utf8; Query OK, 0 rows affected (0.01 sec) mysql> set collation_server=utf8; Query OK, 0 rows affected (0.01 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.03 sec) mysql> show variables like 'collation_%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+ 3 rows in set (0.04 sec)
?
OK,问题解决!