一、页面提交到tomcat乱码
以tomcat6.0.32为例,需将以下代码:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
如果通过tomcat远程调试,发现tomcat端接受到的数据不乱码,入库后乱码,则有可能是数据库设置造成的(同时也要检查操作系统
对于Mysql数据库不同的版本配置也不一样。
安装完数据库后,登录数据库,通过如下命令查看,看数据库的字符集是否正确:
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.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/ | +--------------------------+----------------------------+
查找安装完成后的数据库cnf文件
#find / -name *.cnf /usr/share/mysql/my-large.cnf /usr/share/mysql/my-small.cnf /usr/share/mysql/my-innodb-heavy-4G.cnf /usr/share/mysql/my-medium.cnf /usr/share/mysql/my-huge.cnf #cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #vim /etc/my.cnf
在[mysqld]下增加character_set_server=utf8
[client] #password = your_password port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set=utf8 # Here follows entries for some specific programs # The MySQL server [mysqld] character_set_server=utf8 port = 3306 socket = /var/lib/mysql/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M
#/etc/init.d/mysql restart
#more /var/lib/mysql/...err
所以并不是/var/lib/mysql/mysql.sock不存在造成的,如果/etc/my.cnf正确无误,
则执行/etc/init.d/mysql restart的时候mysql.sock会自动生成的,mysql.sock无法查看,无法修改,即便是用root用户。
此时就要检查/etc/my.cnf设置是否正确,比如上文提到过的mysql不同版本对于[mysqld]节点的字符集设置是default-character-
正确设置后,再运行/etc/init.d/mysql restart,提示成功,系统会自动在/var/lib/mysql下生成一个mysql.sock文件。
此时再进入到mysql中,通过 show variables like 'collation_%';和show variables like 'character_set_%';结果是否正确。
mysql: Switching to the default character set 'latin1'.
查看操作系统的字符集
#locale LANG=utf8 LC_CTYPE="utf8" LC_NUMERIC="utf8" LC_TIME="utf8" LC_COLLATE="utf8" LC_MONETARY="utf8" LC_MESSAGES="utf8" LC_PAPER="utf8" LC_NAME="utf8" LC_ADDRESS="utf8" LC_TELEPHONE="utf8" LC_MEASUREMENT="utf8" LC_IDENTIFICATION="utf8" LC_ALL=
#vim /etc/syconfig/i18n
另外,再查看一下环境变量的设置
#vim /etc/profile
#export LANG="zh_CN.UTF-8"
#source /etc/profile
三、hibernate设置
如果hibernate中文乱码,可以尝试在persistence.xml中增加
hibernate配置文件中,加上属性
<property name="connection.useUnicode">true</property> <property name="connection.characterEncoding">UTF-8</property>
CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;)
shell> mysql --user=root mysql mysql> GRANT ALL PRIVILEGES ON *(or DBName).* TO monty@localhost IDENTIFIED BY 'something' WITH GRANT OPTION; mysql> GRANT ALL PRIVILEGES ON *(or DBName).* TO monty@"%" IDENTIFIED BY 'something' WITH GRANT OPTION; mysql> GRANT RELOAD,PROCESS ON *(or DBName).* TO admin@localhost; mysql> GRANT USAGE ON *.* TO dummy@localhost;