在网上下载了一个简单的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>
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>
解决方案 »
- spring security3 整合CAS 登陆问题 求解决
- java连接局域网中的Oracle数据库应该在Hibernate中怎么配置
- jbpm 如何获得下一个节点的类型
- 如何得到MediaPlay播放时间!!!
- java程序修改,急!!!
- Help Me
- 百分求JAVA简单代码,高手请进!!!
- Spring中dao泛型如何继承?
- 难道就没有人用JBuilder5.0+BAS4.5写过ejb吗?请进(你想要多少分?)
- 跪求高手指导hibernate此hql 怎么写
- 紧急求助js读取属性名包含空格的问题,在线等。。。
- 什么是抽像类!抽像类和非抽像类有什么区别!抽像类相对于非抽像类有什么优点!
改为native,identity等,都试过了,还是报同样的错误,这个问题困扰很久了,有遇到的朋友帮帮忙!
谢谢啦~~~~~~~
<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中就行了
难道我需要手动的为这个程序建数据库mytest,然后建表users,然后建相应的字段么?如果这样子的话为啥还需要写那个User.hbm.xml!
谢谢指点!
<property name="hbn2ddl_auto">create-drop</property>
这样就可以了,还有一个update属性也可以
还有就是 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>
另外,那个错误会和这个有关么?!
但不明白为啥没数据库的时候Hibernate建表总是失败
--------------------------------------------------
数据库要手动建.表则按照Hibernate配置生成!