0 0

mysql 中文字段乱码5

1、我在mysql数据库中建了一张中文字段的表
create table test(
    姓名 varchar(20)
)

2、然后用jdbc中的PreparedStatement对象执行插入或修改操作报错,好像sql中的中文列名变成了乱码,不能成功执行。
部分sql语句如下:
    PreparedStatement pstmt = conn.prepareStatement("insert into test(姓名) values('00001')");
或者
     PreparedStatement pstmt = conn.prepareStatement("update test set `姓名`='1111'");
然后执行:
    pstmt.executeUpdate();
报如下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??) values('00001')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
        ……………………

最近操作别人公司的mysql业务数据库,其中所有表的字段都是中文的,我有没办法改,所以必须使用中文的,没到万不得已,我一般打死都不在数据库中用中文(作为一个中国人的悲哀!!)。

请哪位帮忙解决下,急用!!!!!!!!
先感谢!!!!
DAO 
2011年10月27日 09:22

4个答案 按时间排序 按投票排序

0 0

采纳的答案

连接串的问题,
在数据库连接串中加上编码设置:

String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312 ";

2011年10月27日 11:46
0 0

你先在数据库中写一个成功的sql语句嘛,然后把sql语句粘贴到程序中不是就行了嘛 。

2011年10月27日 13:50
0 0

字段名用中文确实很奇怪。。。。。
数据库建表的时候字符集设为utf-8试试

create table test(
    姓名 varchar(20)
)
default charset=utf8;

然后

PreparedStatement pstmt = conn.prepareStatement("update test set `姓名`='1111'"); 


你这句里是不是标点用错了 “姓名”两边的单引号还是要在英文输入法的'',不是中文输入法下‘’

2011年10月27日 10:19
0 0

简单的方法是在连接数据库成功后,在查询操作前先执行
mysql_query("set character set gb2312");

mysql_query("SET NAMES gb2312");

2011年10月27日 10:17

相关推荐

Global site tag (gtag.js) - Google Analytics