google-code-export / nutz

Automatically exported from code.google.com/p/nutz
Apache License 2.0
1 stars 1 forks source link

中文录入乱码 #174

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
问题产生的条件:
   mysql数据库,数据库所有的编码都为utf-8,java的编码也为utf-8

发生问题的调用代码:
     Dao dao = new NutDao(new DataSource()); 
        Person p = new Person(); 
        p.setName("中国"); 
        p.setAge(20); 
        dao.insert(p);

异常堆栈:
Exception in thread "main" ERROR: 10-05-03 19:21:42
org.nutz.dao.DaoException: !Nuz SQL Error: 'INSERT INTO t_person(age,name)
VALUES( 20, '中国')'
ERROR: 10-05-03 19:21:42    at org.nutz.dao.sql.SqlImpl.execute(SqlImpl.java:117)
ERROR: 10-05-03 19:21:42    at org.nutz.dao.impl.NutDao$2.invoke(NutDao.java:230)
ERROR: 10-05-03 19:21:42    at
org.nutz.dao.ConnectionHolder.invoke(ConnectionHolder.java:37)
ERROR: 10-05-03 19:21:42    at
org.nutz.dao.impl.DefaultDaoRunner.run(DefaultDaoRunner.java:17)
ERROR: 10-05-03 19:21:42    at org.nutz.dao.impl.NutDao.run(NutDao.java:243)
ERROR: 10-05-03 19:21:42    at org.nutz.dao.impl.NutDao.execute(NutDao.java:209)
ERROR: 10-05-03 19:21:42    at
org.nutz.dao.impl.NutDao._insertSelf(NutDao.java:617)
ERROR: 10-05-03 19:21:42    at org.nutz.dao.impl.NutDao.access$5(NutDao.java:613)
ERROR: 10-05-03 19:21:42    at
org.nutz.dao.impl.NutDao$11.invoke(NutDao.java:642)
ERROR: 10-05-03 19:21:42    at org.nutz.lang.Lang.each(Lang.java:983)
ERROR: 10-05-03 19:21:42    at org.nutz.dao.impl.NutDao.insert(NutDao.java:640)
ERROR: 10-05-03 19:21:42    at lzstest.TestNutz.main(TestNutz.java:28)
ERROR: 10-05-03 19:21:42 Caused by: java.sql.SQLException: Incorrect string
value: '\xAD\xE5\x9B\xBD' for column 'name' at row 1

相关日志:

Original issue reported on code.google.com by xiaozhuw...@gmail.com on 3 May 2010 at 11:22

GoogleCodeExporter commented 9 years ago

执行到这句:然后有问题啦:

stat.execute();  [ 这条语句在文件sqlImpl.java]中

其中的stat还是 com.mysql.jdbc.PreparedStatement@143a083: INSERT INTO
t_person(age,name) VALUES( 20, '中国')

所以,应该是 PreparedStatement  stat 
的问题,从这里可以看到,应该是datasource的问题啦

Original comment by xiaozhuw...@gmail.com on 3 May 2010 at 12:40

GoogleCodeExporter commented 9 years ago
中文乱码问题解决,不是nutz的问题,实在不好意思,是我程�
��中设置了硬编码为GBK

具体原因是我在程序中设置连接池的时候预先执行了 set names 
'gbk';

Original comment by xiaozhuw...@gmail.com on 3 May 2010 at 1:33

GoogleCodeExporter commented 9 years ago
用户的代码导致的问题.

Original comment by wendal1985@gmail.com on 3 May 2010 at 1:35

GoogleCodeExporter commented 9 years ago

Original comment by wendal1985@gmail.com on 13 May 2010 at 11:03