使用Eclipse3.2+MyEclipse5.1开发Spring+Struts+Hibernate应用,使用自动生成的applicationContext.xml和hbm可能文件会出现以下两个问题:
1、在进行数据库操作时,发生连接数据库错误,异常信息大致如下:
org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
........(省略部分信息)
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL '
jdbc:mysql://localhost:3306/cdstore?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
'
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:298)
... 29 more
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
... 32 more
修改方法:
在applicationContext.xml把配置DataSource的部分全部改为一行,即有
<property name="user">
<value>test</value>
</property>
改为
<property name="username" value="test" />
例如:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="test" />
<property name="password" value="test" />
</bean>
2、创建数据库连接成功,但进行操作时,又抛出异常,异常信息大致如下:
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
.............(省略部分信息)
Caused by: java.sql.BatchUpdateException: Base table or view not found, message from server: "Table 'test.test__user' doesn't exist"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1404)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
要操作的表是user,而这里却变成了test__user,应该是把数据库的名字也加了上去。检查hbm文件,发现其中有一个catalog属性,去掉即可。如下:
<hibernate-mapping>
<class name="com.test.domain.User" table="user" catalog="test">
改为:
<hibernate-mapping>
<class name="com.test.domain.User" table="user">
重新发布,ok!