第一种情况:mysql能插入中文,但是java查询、更新处理中文乱码
1,找到 Mysql 安装目录下的 my.ini 文件,用记事本打开:如果你的文件某部分与下面的部分内容一致
[mysql]
default-character-set= latin1
--------------------------------------------------------------------------------------------------
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"
#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set= latin1
那么,插入或更新数据到mysql数据库的中文处理:
str=new String(str.getBytes("gb2312"),"ISO-8859-1");
从mysql数据库查询出来中文处理:
str=new String(rs.getString(2).getBytes("ISO-8859-1"),"gb2312");
第二种情况,mysql不能插入中文,而且java查询、更新数据库出现中文乱码
参考:
http://developer.51cto.com/art/200906/130425.htm
1,
设置
Mysql
的编码格式(注意是在创建数据库之前就要设置)
找到
Mysql
安装目录下的
myini
文件,用记事本打开,找到以下两句改为以下的值:
[mysql]
default-character-set=
gb2312
--------------------------------------------------------------------------------------------------
[mysqld]
# The TCP/IP Port the MySQL Server will listen on
port=3306
#Path to installation directory. All paths are usually resolved relative to this.
basedir="C:/Program Files/MySQL/MySQL Server 5.0/"
#Path to the database root
datadir="C:/Program Files/MySQL/MySQL Server 5.0/Data/"
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=
utf8
2,打开mysql,在mysql那里查询一下,应该会是以下的结果。
show variables
like
'character%'
;
+
--------------------------+--------------------------+
| Variable_name | Value |
+
--------------------------+--------------------------+
| character_set_client | gb2312 |
| character_set_connection | gb2312 |
| character_set_database | utf8 |
| character_set_filesystem |
binary
|
| character_set_results | gb2312 |
| character_set_server | utf8 |
| character_set_system | utf8
|
+
--------------------------+--------------------------+
3,然后在MYSQL插入中文应该没有问题了吧。
4,在java查询、插入、修改中文数据的时候进行这样的处理:
String str=new String(t[3].getText().getBytes("GBK"),"gb2312");
至于为什么是“GBK”我也还没有弄清楚,反正其他的不行。。。