Java – run a script to create a table using HSQLDB

I use HSQLDB to run unit tests that require database access

At present, 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();
    }
}@H_404_5@ 
 

getConnection()方法检索Spring上下文中定义的DataSource:

<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>@H_404_5@ 
 

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

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

我在HsqlDB文档中看到我可以问他run a script at the startup.但是,它不符合我的要求,因为我想在运行时运行脚本.

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

任何的想法?

Solution

Because you are already using spring, you may want to use simplejdbcutils. Exe, which executes SQL scripts Executessqlscript method, where statements are separated by semicolons This class is in the spring test module (jar)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>