SimpleJdbcTestUtils.executeScript和多行脚本

时间:2010-02-19 13:13:25

标签: java spring jdbc hsqldb

我想为我的单元测试加载SQL脚本文件。当我使用Spring 2.5.2时,我decided使用SimpleJdbcTestUtils.executeScript()方法加载我的脚本文件,使用以下代码:

DriverManagerDataSource dataSource = ... // getting my DataSource defined in my Spring context
SimpleJdbcTemplate template = new SimpleJdbcTemplate(dataSource);
Resource resource = new ClassPathResource("/create-table.sql");
SimpleJdbcTestUtils.executeSqlScript(template, resource, true);

如果我在create-table.sql文件的一行中编写每个SQL语句,那么一切正常。但是如果我在多行上写一个语句,那么即使语句由;完成,我也会收到错误。

工作脚本:

CREATE TABLE T_FOO (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP, IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE T_BAR (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP, IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);

不工作的脚本:

CREATE TABLE T_FOO (
    ID NUMERIC PRIMARY KEY,
    DATEID TIMESTAMP,
    IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);
CREATE TABLE T_BAR (
    ID NUMERIC PRIMARY KEY,
    DATEID TIMESTAMP,
    IS_ACTIVE INTEGER DEFAULT 0 NOT NULL);

第二个文件出错:

org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [CREATE TABLE T_FOO (]; nested exception is java.sql.SQLException: Unexpected token:  in statement [CREATE TABLE T_FOO (]
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:111)
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:404)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:519)
    at org.springframework.jdbc.core.simple.SimpleJdbcTemplate.update(SimpleJdbcTemplate.java:237)
    at org.springframework.test.jdbc.SimpleJdbcTestUtils.executeSqlScript(SimpleJdbcTestUtils.java:150)
    at org.springframework.test.jdbc.SimpleJdbcTestUtils.executeSqlScript(SimpleJdbcTestUtils.java:113)
    at foo.bar.HsqldbUtils.run(HsqldbUtils.java:95)
    at foo.bar.SomeUnitTest.executeTests(SomeUnitTest.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: Unexpected token:  in statement [CREATE TABLE T_FOO (]
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.jdbcStatement.executeUpdate(Unknown Source)
    at org.springframework.jdbc.core.JdbcTemplate$1UpdateStatementCallback.doInStatement(JdbcTemplate.java:509)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:393)

由于我愿意创建更复杂的表,我更喜欢以更易读的方式编写SQL指令,因此使用多行。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:6)

您使用的是Spring 2.5.2,但请参阅2.5.6文档。

如果您查看2.5.2的javadoc(请参阅here),您会注意到它不支持多行脚本。

这显然已在2.5.4(问题here)中得到解决。

你应该升级到2.5.6,或者更好,3.0.x。