Java 运行脚本以使用 HSQLDB 创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2295457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:47:57  来源:igfitidea点击:

Running a script to create tables with HSQLDB

javasqlhsqldb

提问by Romain Linsolas

I use hsqldbto run my unit tests that need a database access.

我使用hsqldb来运行需要访问数据库的单元测试。

For the moment, when I want to create a table for a specific test, I have the following code:

目前,当我想为特定测试创建表时,我有以下代码:

private void createTable() {
    PreparedStatement ps;
    try {
        ps = getConnection().prepareStatement("CREATE TABLE T_DATE (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP)");
        ps.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

The getConnection()method retrieve a DataSource defined in a Springcontext:

getConnection()方法检索在Spring上下文中定义的数据源:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:mem:memoryDB"/>
    <property name="username" value="SA"/>
    <property name="password" value=""/>
</bean>

Now, I want to create my table from a SQL script (of course, this script will contain more than one table creation):

现在,我想从一个 SQL 脚本创建我的表(当然,这个脚本将包含多个表创建):

CREATE TABLE T_DATE_FOO (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP);
CREATE TABLE T_DATE_BAR (ID NUMERIC PRIMARY KEY, DATEID TIMESTAMP);
...

I've seen in the HSQLDB documentation that I can ask him to run a script at the startup. However, it does not meet my requirements, as I want to run a script at the runtime.

我在 HSQLDB 文档中看到我可以让他在启动时运行脚本。但是,它不符合我的要求,因为我想在运行时运行脚本。

Of course, I can read the file myself, and for every SQL statement, I run a ps.executeUpdate()command, but I don't want to use this kind of solution (except if there are no other solution).

当然,我可以自己读取文件,对于每个SQL语句,我都运行一个ps.executeUpdate()命令,但我不想使用这种解决方案(除非没有其他解决方案)。

Any idea?

任何的想法?

采纳答案by Stefan De Boey

since you're already using spring, you might want to use the SimpleJdbcUtils.executeSQLScriptmethod which executes an SQL script where the statements are separated with semicolon. this class is in the spring-test module (JAR).

由于您已经在使用 spring,您可能想要使用SimpleJdbcUtils.executeSQLScript方法,该方法执行 SQL 脚本,其中语句用分号分隔。这个类在 spring-test 模块 (JAR) 中。

回答by artemb

I had the same problem. I ended up splitting the text file by ; and executing each statement separately. It was OK because we had no inserts therefore no semicolons inside statements. I haven't found an easy way to run an SQL script at that time

我有同样的问题。我最终将文本文件拆分为 ; 并分别执行每个语句。没关系,因为我们没有插入,因此语句中没有分号。当时我还没有找到一种简单的方法来运行 SQL 脚本

回答by Adisesha

First of all, I do not know implications of this. I used it long time back it worked for me. The SQLExec class is from ant.jar, you can probably look into the ant source to create your own utility class,

首先,我不知道这意味着什么。我用了很长时间,它对我有用。SQLExec 类来自 ant.jar,您可以查看 ant 源代码来创建您自己的实用程序类,

SQLExec sqlExec=new SQLExec();
sqlExec.setUserid("user");
sqlExec.setPassword("passowrd");
sqlExec.setUrl("jdbc:mysql://localhost:3306/dbname");
sqlExec.setDriver("com.mysql.jdbc.Driver");
sqlExec.setProject(new Project());
sqlExec.setSrc(new File("c:/test.sql"));
sqlExec.execute();

回答by Ralf Huthmann

You might give org.hsqldb.util.SqlFilea try. This class seems to be a perfect match for your problem.

你可以org.hsqldb.util.SqlFile试试。这门课似乎非常适合您的问题。