【问题标题】:Spring jdbc:initialize-database not workingSpring jdbc:初始化数据库不起作用
【发布时间】:2012-06-10 16:57:53
【问题描述】:

我无法让 spring 初始化我的数据库,尽管据我所知,我这样做是正确的。我不断收到 org/springframework/dao/DataAccessResourceFailureException。

这是我的 springapp-servlet.xml:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>



<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="classpath:create_products.sql" />
<jdbc:script location="classpath:load_data.sql" />
 </jdbc:initialize-database>

jdbc.properties 文件位于 Java Resources/src 中,是这样的:

 jdbc.driverClassName=org.hsqldb.jdbcDriver
 jdbc.url=jdbc:hsqldb:hsql://localhost
 jdbc.username=sa
 jdbc.password=

create_products.sql 也在 Java Resources/src 中,是这样的: 创建表产品( id INTEGER NOT NULL 主键, 描述 varchar(255), 价格小数(15,2) );

load_data.sql 在同一个位置,是这样的:

INSERT INTO products (id, description, price) values(1, 'Lamp', 5.78);
INSERT INTO products (id, description, price) values(2, 'Table', 75.29);
INSERT INTO products (id, description, price) values(3, 'Chair', 22.81);

有什么建议吗? 谢谢。

【问题讨论】:

  • 我会推荐 Hibernate 来完成这项任务.. 但如果你不能,我认为我能给出的最好建议是获取 log4j 并进行一些日志记录。有了完整的堆栈跟踪,可能会有有人会知道会发生什么。
  • 除了初始化,还能访问数据库吗?使用您的 jdbc.url,您需要在运行代码之前单独启动 HSQLDB 服务器。
  • @Dave : 你的 spring 配置文件 'springapp-servlet.xml' 的位置在哪里?

标签: spring spring-mvc hsqldb


【解决方案1】:

Dave 可能您的 springapp-servlet.xml cvc-complex-type.2.4.b: The content of element 'jdbc:initialize-database' is not complete. One of '{"http://www.springframework.org/schema/jdbc":script}' is expected. 中也出现此错误?

org.springframework.jdbc.datasource.init 包提供对初始化现有数据源的支持。只需检查您的 spring-jdbc 版本。 参考:[http://static.springsource.org/spring/docs/3.0.0.RC3/reference/html/ch12s09.html][1]

只需打开spring-jdbc jar 并在下面查看:org/springframework/jdbc/datasource 如果您可以看到任何名称为init 的文件夹。如果init 文件夹不存在,那么您使用的Spring Framework 版本不支持此功能功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多