彻底解决乱码问题JAVA

真是很有用的一篇文章,转自:http://xupo.iteye.com/blog/1135891,解决了我的数据库乱码问题,其它还没有试过,保留下来。

每次做中文项目时,必定会遇到的一个问题,就是中文显示乱码问题,每次的解决方式都不太相同。 这次将GK项目过程中遇到乱码问题及解决方式记录下来,以备忘。 乱码的可以有多个环节,如页面提交到tomcat乱码,tomcat到数据库乱码,数据库提取显示乱码。

一、页面提交到tomcat乱码 解决方法是在tomcat/conf/server.xml中进行配置, 以tomcat6.0.32为例,需将以下代码:

Xml代码
  1. <Connectorport="8080"protocol="HTTP/1.1" 
  2.                connectionTimeout="20000" 
  3.                redirectPort="8443"/> 
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

 

改为:

Xml代码
  1. <Connectorport="8080"protocol="HTTP/1.1" 
  2.                connectionTimeout="20000" 
  3.                redirectPort="8443"URIEncoding="UTF-8"/> 
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8" />

 

如果tomcat前端有Apache或是Nginx转发,则还需将:

Xml代码
  1. <Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/> 
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

 

改为:

Xml代码
  1. <Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"URIEncoding="UTF-8"/> 
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />

 

二、tomcat到数据库乱码 如果通过tomcat远程调试,发现tomcat端接受到的数据不乱码,入库后乱码,则有可能是数据库设置造成的(同时也要检查操作系统

的字符集)。 对于Mysql数据库不同的版本配置也不一样。 安装完数据库后,登录数据库,通过如下命令查看,看数据库的字符集是否正确:

 

Shell代码
  1. mysql> show variables like 'collation_%';  
  2. +----------------------+-----------------+  
  3. | Variable_name        | Value           |  
  4. +----------------------+-----------------+  
  5. | collation_connection | utf8_general_ci |  
  6. | collation_database   | utf8_general_ci |  
  7. | collation_server     | utf8_general_ci |  
  8. +----------------------+-----------------+  
  9. 3 rows in set (0.00 sec)  
  10. mysql> show variables like 'character_set_%';  
  11. +--------------------------+----------------------------+  
  12. | Variable_name            | Value                      |  
  13. +--------------------------+----------------------------+  
  14. | character_set_client     | utf8                       |  
  15. | character_set_connection | utf8                       |  
  16. | character_set_database   | utf8                       |  
  17. | character_set_filesystem | binary                     |  
  18. | character_set_results    | utf8                       |  
  19. | character_set_server     | utf8                       |  
  20. | character_set_system     | utf8                       |  
  21. | character_sets_dir       | /usr/share/mysql/charsets/ |  
  22. +--------------------------+----------------------------+ 
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文件

Shell代码
  1. #find / -name *.cnf  
  2. /usr/share/mysql/my-large.cnf  
  3. /usr/share/mysql/my-small.cnf  
  4. /usr/share/mysql/my-innodb-heavy-4G.cnf  
  5. /usr/share/mysql/my-medium.cnf  
  6. /usr/share/mysql/my-huge.cnf  
  7. #cp /usr/share/mysql/my-medium.cnf /etc/my.cnf  
  8. #vim /etc/my.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

 

在[client]下增加default-character-set=utf8 在[mysqld]下增加character_set_server=utf8

一定注意,这里不是default-character-set=utf8(对于5.5以前的版本应该写

default-character-set=utf8, 5.5以后必须写character_set_server=utf8,否则无效

设置完时内容如下:(注:这里的其他数值未根据实际情况修改)

Cnf代码
  1. [client]  
  2. #password       = your_password  
  3. port            = 3306 
  4. socket          = /var/lib/mysql/mysql.sock  
  5. default-character-set=utf8  
  6.  
  7. # Here follows entries for some specific programs  
  8.  
  9. # The MySQL server  
  10. [mysqld]  
  11. character_set_server=utf8  
  12. port            = 3306 
  13. socket          = /var/lib/mysql/mysql.sock  
  14. skip-external-locking  
  15. key_buffer_size = 16M  
  16. max_allowed_packet = 1M  
  17. table_open_cache = 64 
  18. sort_buffer_size = 512K  
  19. net_buffer_length = 8K  
  20. read_buffer_size = 256K  
  21. read_rnd_buffer_size = 512K  
  22. myisam_sort_buffer_size = 8M 
[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

 

修改完成后,可以通过以下命令重启mysql:

Shell代码
  1. #/etc/init.d/mysql restart 
#/etc/init.d/mysql restart

 

如果在重启过程中看到如下错误信息:

Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’

 

或是其他类似The server quit without updating PID file 之类的错误,都可以通过进一步查看详细错误信息:

Shell代码
  1. #more /var/lib/mysql/...err 
#more /var/lib/mysql/...err

 

也许你会看到类似如下的错误信息:

unknown variable 'default-character-set=utf8'

 

这说明/etc/my.cnf配置出错了。 所以并不是/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-

set=utf8还是character_set_server=utf8,这是不一样的。 正确设置后,再运行/etc/init.d/mysql restart,提示成功,系统会自动在/var/lib/mysql下生成一个mysql.sock文件。 此时再进入到mysql中,通过 show variables like 'collation_%';和show variables like 'character_set_%';结果是否正确。

有时候,在linux SHELL中输入mysql会有类似mysql无法识别OS Characterset GB18030,

mysql: Unknown OS character set 'GB18030'. mysql: Switching to the default character set 'latin1'.

 

采用默认的latin1之类的信息,表明操作系统的编码设置需要修改,描述如下: 查看操作系统的字符集

Shell代码
  1. #locale  
  2. LANG=utf8  
  3. LC_CTYPE="utf8" 
  4. LC_NUMERIC="utf8" 
  5. LC_TIME="utf8" 
  6. LC_COLLATE="utf8" 
  7. LC_MONETARY="utf8" 
  8. LC_MESSAGES="utf8" 
  9. LC_PAPER="utf8" 
  10. LC_NAME="utf8" 
  11. LC_ADDRESS="utf8" 
  12. LC_TELEPHONE="utf8" 
  13. LC_MEASUREMENT="utf8" 
  14. LC_IDENTIFICATION="utf8" 
  15. LC_ALL= 
#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=

 

如果不是这样,则需要对操作系统字符集进行修改:

Shell代码
  1. #vim /etc/syconfig/i18n 
#vim /etc/syconfig/i18n

 

修改成:LANG="zh_CN.UTF-8"即可 另外,再查看一下环境变量的设置

Shell代码
  1. #vim /etc/profile 
#vim /etc/profile

 

里面可以没有字符集的设置,如果有,应类似如下设置:

export LC_ALL="C" #export LANG="zh_CN.UTF-8"

 

设置完成后,

Shell代码
  1. #source /etc/profile 
#source /etc/profile

 

使环境变量生效,如果不放心,可以reboot

三、hibernate设置 如果hibernate中文乱码,可以尝试在persistence.xml中增加 hibernate配置文件中,加上属性

Xml代码
  1. <propertyname="connection.useUnicode">true</property>   
  2. <propertyname="connection.characterEncoding">UTF-8</property>  
<property name="connection.useUnicode">true</property> 
<property name="connection.characterEncoding">UTF-8</property>

 

URL连接可以设置成:jdbc:mysql://localhost:3306/workshopdb?useUnicode=true&characterEncoding=UTF-8(如果其他方式能解决

乱码,就不这么设置)

乱码的可能性还很多,具体问题还得具体分析。

 

补充、数据库相关

如果要通过命令创建UTF-8格式的数据库,需通过如下方式:

Sql代码
  1. CREATEDATABASE `test2` DEFAULTCHARACTERSET utf8 COLLATE utf8_general_ci 
CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

 

如果是GBK,则用

Sql代码
  1. createdatabase test2 DEFAULTCHARACTERSET gbk COLLATE gbk_chinese_ci;) 
create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;)

 

如果需要为Mysql增加用户

 

Shell代码
  1. shell> mysql --user=root mysql  
  2. mysql> GRANT ALL PRIVILEGES ON *(or DBName).* TO monty@localhost  
  3. IDENTIFIED BY 'something' WITH GRANT OPTION;  
  4. mysql> GRANT ALL PRIVILEGES ON *(or DBName).* TO monty@"%" 
  5. IDENTIFIED BY 'something' WITH GRANT OPTION;  
  6. mysql> GRANT RELOAD,PROCESS ON *(or DBName).* TO admin@localhost;  
  7. mysql> GRANT USAGE ON *.* TO dummy@localhost
posted @ 2012-09-10 16:43 Jehovah 阅读(...) 评论(...) 编辑 收藏