मैं अपने यूनिट परीक्षणों के लिए SQL स्क्रिप्ट फ़ाइलों को लोड करना चाहता हूं। जैसा कि मैंने वसंत 2.5.2 का उपयोग कर रहा है, मैं decided मेरी स्क्रिप्ट फ़ाइल को लोड करने SimpleJdbcTestUtils.executeScript()
विधि का उपयोग करने के लिए, निम्न कोड का उपयोग:SimpleJdbcTestUtils.executeScript और multilines स्क्रिप्ट
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);
काम नहीं कर स्क्रिप्ट:
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)
जैसा कि मैंने और अधिक जटिल तालिकाएं बनाने के लिए तैयार हूँ, यह करें: दूसरी फ़ाइल पर
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);
त्रुटि एसक्यूएल निर्देश को एक और अधिक पठनीय तरीके से लिखें, इसलिए कई लाइनों का उपयोग करें। क्या ऐसा करने के लिए कोई रास्ता है?
डब्ल्यूटीएफ: http://efreedom.com/Question/1-2296468/SimpleJdbcTestUtilsexecuteScript-Multilines-Script? –
@ बोरीस इस साइट में SO प्रश्न की एक प्रति है ... इसके बारे में कुछ जांच की आवश्यकता है। धन्यवाद। – romaintaz
@ बोरीस इसे देखो: http://meta.stackexchange.com/questions/58369 यह ठीक लगता है ... – romaintaz