首页 > 代码库 > java向数据库中插入中文出现乱码

java向数据库中插入中文出现乱码

引用 http://www.cnblogs.com/jack204/archive/2012/09/11/2680106.html

 

主要解决方向,JAVA与MYSQL中编码要统一。通常采用UTF-8.

这里虽然你在项目中设置了数据库采用UTF-8,但是那里不包括连接方式之类的。

 

1.查询自身MYSQL编码方式

MySQL默认编码是latin1

mysql> show variables like ‘character%‘;  
+--------------------------+--------------------------+  
| 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       | D:\MySQL\share\charsets\ |  
+--------------------------+--------------------------+  

 

2.若项目用UTF8编码,则将MYSQL也改为UTF8编码。通过修改my.ini文件。添加如下几项,再重启MYSQL服务就好。

[mysql]
default-character-set = utf8

[mysql.server]
default-character-set = utf8


[mysqld_safe]
default-character-set = utf8


[client]
default-character-set = utf8
[mysqld]
character-set-server=utf8 
collation-server=utf8_general_ci 

 

java向数据库中插入中文出现乱码