MyEclipse5.1开发Spring Struts Hibernate应用时的数据库问题

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 使用Eclipse3.2+MyEclipse5.1开发Spring+Struts+Hibernate应用,使用自动生成的applicationContext.xml和hbm可能文件会出现以下两个问题:1、在进行数据库操作时,发生连接数据库错误,异常信息大致如下:org.
使用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&amp;useUnicode=true&amp;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! 
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
27天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
11 0
|
27天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
10 0
|
27天前
|
Shell
sh(Spring+Spring mvc+hibernate)——IEmpDao.java
sh(Spring+Spring mvc+hibernate)——IEmpDao.java
10 0
|
27天前
|
Shell
sh(Spring+Spring mvc+hibernate)——IDeptDao.java
sh(Spring+Spring mvc+hibernate)——IDeptDao.java
12 0
|
27天前
|
JavaScript 前端开发 Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Jsp页面
9 0
|
27天前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——OpSessionview实现
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——OpSessionview实现
12 0
|
27天前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——EmpDao层代码
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——EmpDao层代码
16 0
|
27天前
|
Java
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Action的实现类
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——Action的实现类
10 0
|
27天前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——工具类
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——工具类
10 0
|
27天前
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——DeptDao层代码
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——DeptDao层代码
9 0

推荐镜像

更多