java - 续行 Spring SQL ScriptUtils(使用 H2)

标签 java sql spring jdbc h2

我有以下 SQL 脚本 (initDB.sql)

CREATE TABLE  FFShareHistorical  (  ID int NOT NULL AUTO_INCREMENT,
                                    PX_LAST  Double DEFAULT NULL, 
                                    PX_OPEN  Double DEFAULT NULL, 
                                    PX_HIGH  Double DEFAULT NULL, 
                                    PX_LOW  Double DEFAULT NULL, 
                                    PRIMARY KEY (ID))

并希望使用 Spring 的 ScriptUtils (4.1.4.RELEASE) 来执行它,即

Resource rc = new ClassPathResource("initDB.sql");
ScriptUtils.executeSqlScript(dataSource.getConnection(), rc);

问题在于,换行符似乎被解释为 SQL 语句的终止,即错误堆栈跟踪看起来像这样

org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [initDB.sql]: CREATE TABLE FFShareHistorical ( ID int NOT NULL AUTO_INCREMENT, ; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE FFSHAREHISTORICAL ( ID INT NOT NULL AUTO_INCREMENT,  "; expected "identifier"; SQL statement:
CREATE TABLE FFShareHistorical ( ID int NOT NULL AUTO_INCREMENT,  [42001-176]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:344)
    at org.h2.message.DbException.getSyntaxError(DbException.java:204)
    at org.h2.command.Parser.readColumnIdentifier(Parser.java:3068)
    at org.h2.command.Parser.parseCreateTable(Parser.java:5722)
    at org.h2.command.Parser.parseCreate(Parser.java:4122)
    at org.h2.command.Parser.parsePrepared(Parser.java:351)
    at org.h2.command.Parser.parse(Parser.java:306)
    at org.h2.command.Parser.parse(Parser.java:278)
    at org.h2.command.Parser.prepareCommand(Parser.java:243)
    at org.h2.engine.Session.prepareLocal(Session.java:442)
    at org.h2.server.TcpServerThread.process(TcpServerThread.java:265)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:160)
    at java.lang.Thread.run(Unknown Source)

    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:475)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:393)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:372)
    at ch.qpmlib.flatfiledatabase.jdbc.dao.FFDBSetup.setupDB(FFDBSetup.java:26)
    at ch.qpmlib.flatfiledatabase.jdbc.main.SpringMain.setupDB(SpringMain.java:30)
    at ch.qpmlib.flatfiledatabase.jdbc.main.SpringMain.main(SpringMain.java:17)
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE FFSHAREHISTORICAL ( ID INT NOT NULL AUTO_INCREMENT,  "; expected "identifier"; SQL statement:
CREATE TABLE FFShareHistorical ( ID int NOT NULL AUTO_INCREMENT,  [42001-176]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:344)
    at org.h2.message.DbException.getSyntaxError(DbException.java:204)
    at org.h2.command.Parser.readColumnIdentifier(Parser.java:3068)
    at org.h2.command.Parser.parseCreateTable(Parser.java:5722)
    at org.h2.command.Parser.parseCreate(Parser.java:4122)
    at org.h2.command.Parser.parsePrepared(Parser.java:351)
    at org.h2.command.Parser.parse(Parser.java:306)
    at org.h2.command.Parser.parse(Parser.java:278)
    at org.h2.command.Parser.prepareCommand(Parser.java:243)
    at org.h2.engine.Session.prepareLocal(Session.java:442)
    at org.h2.server.TcpServerThread.process(TcpServerThread.java:265)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:160)
    at java.lang.Thread.run(Unknown Source)

所以问题是,如何将 ScriptUtils 与多行 SQL 脚本一起使用?特别是我正在寻找某种可以添加到脚本中的“行继续”字符,或者一种告诉 Spring 的 ScriptUtils 删除所有换行符的方法。

最佳答案

事实证明,使用多行语句是没有问题的。但是,如果语句未以“;”结尾,则 ScriptUtils 将退回到使用“\n”作为分隔符。所以在这种情况下,只需使用“;”终止脚本。即

CREATE TABLE  FFShareHistorical  (  ID int NOT NULL AUTO_INCREMENT,
                                    PX_LAST  Double DEFAULT NULL, 
                                    PX_OPEN  Double DEFAULT NULL, 
                                    PX_HIGH  Double DEFAULT NULL, 
                                    PX_LOW  Double DEFAULT NULL, 
                                    PRIMARY KEY (ID));

会完成这项工作。

关于java - 续行 Spring SQL ScriptUtils(使用 H2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30057244/

相关文章:

php - 使用 PHP(或查询)在 MySQL 数据库中使用超过数千行的常用词

java - Spring:如何使服务以 PlatformTransactionManager bean 的可用性为条件?

java - Spring XML 错误 : Could not resolve placeholder 'cas.server.host'

java - 如何让Tomcat8返回503状态码?

sql - 在 PostgreSQL 中获取字符串中子字符串的所有实例?

SQL "like"语句 : 'Jeff' LIKE 'Jeff' = false?

java - 尝试使用 Springs RestTemplate 执行 POST 请求时获取 RestClientException

java - O-Notation 练习题以理解部分代码

java - 通过 Bundle 或 fragment 内定义的公共(public)方法将数据传递给 fragment

java - Scala - 具有简单功能的 java.lang.VerifyError