在网上下载了一个简单的S2SH的整合例子,只有一张表,是对user的增删改查的操作!改完数据库的用户名,密码这些配置外,部署系统,可以去到首页,但是保存数据的时候报错如下:
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot create PoolableConnectionFactory (Unknown database 'mytest'); nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'mytest')
 .......
下面贴出几个配置文件:
首先是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="connection.url">jdbc:mysql://localhost:3306/mytest</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">neverlose</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property></session-factory></hibernate-configuration>然后是applicationContext.xml<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property> 
<property name="username" value="root"></property>
<property name="password" value="neverlose"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>



</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property>

</bean><bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean><bean id="userService" class="com.test.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean><bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean><bean id="listUserAction" class="com.test.action.user.ListUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean><bean id="removeUserAction" class="com.test.action.user.RemoveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean><bean id="updatePUserAction" class="com.test.action.user.UpdatePUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean><bean id="updateUserAction" class="com.test.action.user.UpdateUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean><bean id="generateExcelAction" class="com.test.action.user.GenerateExcelAction" scope="singleton">
<property name="service" ref="userService"></property>
</bean></beans>另外User类和hbm的xml映射文件:package com.test.bean;public class User
{
private Integer id;
private String firstname;
private String lastname;
private int age; public Integer getId()
{
return id;
} public void setId(Integer id)
{
this.id = id;
} public String getFirstname()
{
return firstname;
} public void setFirstname(String firstname)
{
this.firstname = firstname;
} public String getLastname()
{
return lastname;
} public void setLastname(String lastname)
{
this.lastname = lastname;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
}}<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
<class name="com.test.bean.User" table="users">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment"></generator>
</id> <property name="firstname" type="string" column="firstname"
length="50">
</property>
<property name="lastname" type="string" column="lastname"
length="50">
</property>
<property name="age" type="java.lang.Integer" column="age"></property> </class>
</hibernate-mapping>

解决方案 »

  1.   

    之前百度谷歌了下,很多说是自增列的问题,要把  <generator class="increment"></generator>
    改为native,identity等,都试过了,还是报同样的错误,这个问题困扰很久了,有遇到的朋友帮帮忙!
    谢谢啦~~~~~~~
      

  2.   

    不能创建连接池。数据库mytest没有发现。、
      

  3.   

    错误已经提示 mytest,看你是不是建了这个数据库再说吧
      

  4.   

    既然整合了就不用hibernate.cfg.xml: <!-- 定义数据源Bean,使用C3P0数据源实现 -->
    <bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mytest" />
    <property name="user" value="root" />
    <property name="password" value="neverlose"/>
    <property name="maxPoolSize" value="40" />
    <property name="minPoolSize" value="1" />
    <property name="initialPoolSize" value="1" />
    <property name="maxIdleTime" value="20" />
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- mappingResouces属性用来列出全部映射文件 -->
    <property name="mappingResources">
    <list>
    <value>com/test/bean/User.hbm.xml</value> </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLInnoDBDialect
    </prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.format_sql">true</prop>
    </props>
    </property>
    </bean>直接放在applicationcontext.xml中就行了
      

  5.   

    其实我有个地方不明白,Hibernate不是可以根据User类以及其映射文件自动建数据库的么?
    难道我需要手动的为这个程序建数据库mytest,然后建表users,然后建相应的字段么?如果这样子的话为啥还需要写那个User.hbm.xml!
    谢谢指点!
      

  6.   

    你的意思是说那个hibernate.cfg.xml可以去掉么????
      

  7.   

    肯定要建立数据库要建立表啊,不然你去连接哪个数据库找哪个表?User.hbm.xml 只是映射 JavaBean跟表,这样就可以把表看作一个面向对象中的对象进行操作
      

  8.   

    好像不一定吧,问了个同学,他说之前做的一个学籍管理系统也是用了Hibernate,不需要手动建数据库的啊!EJB是可以根据实体类然后自动到数据库里建表的啊,难道Hibernate不可以?!
      

  9.   

    也可以根据对象来创建表,需要配一个hibernate的属性,
    <property name="hbn2ddl_auto">create-drop</property>
    这样就可以了,还有一个update属性也可以
      

  10.   

    数据库肯定要建立的,可以手动建表,自动建表hibernate要加配置语句,楼上都说了。
      

  11.   

    数据库 需要手动创建 表可以通过Hibernate生成;
    还有就是 LZ  你既然配置了 hibernate.cfg.xml
    为啥在 applicationContext.xml 里面又配置了 数据库链接啊?
    其实你在 applicationContext.xml  这样写就可以了:
    <!-- Session 工厂 -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation"
    value="classpath:hibernate.cfg.xml"><!-- 可以不要配置文件(hibernate.cfg.xml),直接写在这个Spring的配置文件中 就像LZ 上面配置的一样-->
    </property>
    </bean>
    <!-- 模板 -->
    <bean id="hibernateTemplate"
    class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 事务器 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
      

  12.   

    谢谢,这个例子是我从网上download的,我也是刚开始学SSH的整合,所以很多地方还需要请教大家!
    另外,那个错误会和这个有关么?!
      

  13.   

    自己手动创建数据库后,没问题!
    但不明白为啥没数据库的时候Hibernate建表总是失败
      

  14.   


    --------------------------------------------------
    数据库要手动建.表则按照Hibernate配置生成!
      

  15.   

    数据库要手动建.表则按照Hibernate配置生成!