这两个问题一起出现,找了好久的Could not execute JDBC batch update问题,网上有提到:
1.因为Hibernate Tools(或者Eclipse本身的Database Explorer)生成*.hbn.xml工具中包含有catalog="***"(*表示数据库名称)这样的属性,将该属性删除就可以了
2.估计是你的列名里面有关键字的原因吧,命名列的时候不要单独使用date,ID...这种关键字
3.可能是驱动jar包有问题,不支持,等等。
我的问题如下:
Exception in thread "main" 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:171)
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 test.Test.main(Test.java:76)
Caused by: java.sql.BatchUpdateException: No database selected
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2015)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 6 more
这两个问题一起出现,找了好久,开始也搞了几个测试,一对多的,一对一的,现在是写多对多,开始那几个都没什么问题,就没考虑到配置文件hibernate.cfg.xml有什么问题。这是我的配置文件:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306
</property>
<property name="connection.username">root</property>
<property name="connection.password">12345678</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysqldb</property>
<!-- <mapping resource="acm/model/Detail.hbm.xml" />
<mapping resource="acm/model/Login.hbm.xml" />
<mapping resource="acm/model/Person.hbm.xml" />
<mapping resource="acm/model/Room.hbm.xml" />-->
<mapping resource="acm/model/Student.hbm.xml" />
<mapping resource="acm/model/Course.hbm.xml" />
</session-factory>
</hibernate-configuration>
后来注意到后面的那个问题才注意这个文件,原来只有加上数据库就行了:
jdbc:mysql://localhost:3306/xscj
但是为什么前面的几个测试都不用这样,这个就要这样,我就不知道了,望大神解决!