xml的dialect换成mysql_Hibernate往MySQL数据库中插入汉字就报错,换成字符就可以。...

这是源代码:

public class addStudent{

public static void main(String[] args) {

Student stu = new Student();

stu.setSid(1);

String str = "中国";

stu.setSname(str);

Configuration  cfg = new Configuration();

cfg.configure();

SessionFactory factory = cfg.buildSessionFactory();

Session  session = factory.openSession();

Transaction tr   = session.beginTransaction();

try {

session.save(stu);

tr.commit();

} catch (Exception e) {

// TODO: handle exception

tr.rollback();

e.printStackTrace();

}

finally{

session.close();

}

}

}

下面是报的异常:

2011-03-23 18:35:05  Hibernate 3.3.2.GA

2011-03-23 18:35:05  hibernate.properties not found

2011-03-23 18:35:05  Bytecode provider name : javassist

2011-03-23 18:35:05  using JDK 1.4 java.sql.Timestamp handling

2011-03-23 18:35:06  configuring from resource: /hibernate.cfg.xml

2011-03-23 18:35:06  Configuration resource: /hibernate.cfg.xml

2011-03-23 18:35:06  Reading mappings from resource : com/pojos/student.hbm.xml

2011-03-23 18:35:06  Mapping class: com.pojos.Student -> student

2011-03-23 18:35:06  Configured SessionFactory: null

2011-03-23 18:35:06  Using Hibernate built-in connection pool (not for production use!)

2011-03-23 18:35:06  Hibernate connection pool size: 20

2011-03-23 18:35:06  autocommit mode: false

2011-03-23 18:35:06  using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=GBK

2011-03-23 18:35:06  connection properties: {user=root, password=****}

2011-03-23 18:35:06  RDBMS: MySQL, version: 5.1.45-community

2011-03-23 18:35:06  JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )

2011-03-23 18:35:06  Using dialect: org.hibernate.dialect.MySQLDialect

2011-03-23 18:35:06  Using default transaction strategy (direct JDBC transactions)

2011-03-23 18:35:06  No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)

2011-03-23 18:35:06  Automatic flush during beforeCompletion(): disabled

2011-03-23 18:35:06  Automatic session close at end of transaction: disabled

2011-03-23 18:35:06  JDBC batch size: 15

2011-03-23 18:35:06  JDBC batch updates for versioned data: disabled

2011-03-23 18:35:06  Scrollable result sets: enabled

2011-03-23 18:35:06  JDBC3 getGeneratedKeys(): enabled

2011-03-23 18:35:06  Connection release mode: auto

2011-03-23 18:35:06  Maximum outer join fetch depth: 2

2011-03-23 18:35:06  Default batch fetch size: 1

2011-03-23 18:35:06  Generate SQL with comments: disabled

2011-03-23 18:35:06  Order SQL updates by primary key: disabled

2011-03-23 18:35:06  Order SQL inserts for batching: disabled

2011-03-23 18:35:06  Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory

2011-03-23 18:35:06  Using ASTQueryTranslatorFactory

2011-03-23 18:35:06  Query language substitutions: {}

2011-03-23 18:35:06  JPA-QL strict compliance: disabled

2011-03-23 18:35:06  Second-level cache: enabled

2011-03-23 18:35:06  Query cache: disabled

2011-03-23 18:35:06  Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory

2011-03-23 18:35:06  Optimize cache for minimal puts: disabled

2011-03-23 18:35:06  Structured second-level cache entries: disabled

2011-03-23 18:35:06  Echoing all SQL to stdout

2011-03-23 18:35:06  Statistics: disabled

2011-03-23 18:35:06  Deleted entity synthetic identifier rollback: disabled

2011-03-23 18:35:06  Default entity-mode: pojo

2011-03-23 18:35:06  Named query checking : enabled

2011-03-23 18:35:06  building session factory

2011-03-23 18:35:06  Not binding factory to JNDI, no JNDI name configured

Hibernate: insert into student (sname, sid) values (?, ?)

2011-03-23 18:35:07  SQL Error: 1366, SQLState: HY000

2011-03-23 18:35:07  Incorrect string value: '\xD6\xD0\xB9\xFA' for column 'sname' at row 1

2011-03-23 18:35:07  Could not synchronize database state with session

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)

at com.test.addStudent.main(addStudent.java:25)

Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA' for column 'sname' at row 1

at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)

at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)

at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)

... 8 more

org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update

at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)

at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)

at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)

at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)

at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)

at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)

at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)

at com.test.addStudent.main(addStudent.java:25)

Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xD6\xD0\xB9\xFA' for column 'sname' at row 1

at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)

at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)

at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)

at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)

... 8 more

高手解决下。。。烦扰我几天了。

2011年3月23日 18:38

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值