Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jdbc initialize-database with Java config

I'm trying to create an hsqldb db from file (I currently have an Embedded database).

What I want is to duplicate this setup:

<bean class="org.apache.commons.dbcp2.BasicDataSource" id="dataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:#{systemProperties['user.home']}/db/data" />
<property name="username" value="sa" />
<property name="password" value="" />

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:scripts/create-table-if-not-exists" />
</jdbc:initialize-database>

The first part was fine, here's my code:

DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
    dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
    dataSource.setUrl("jdbc:hsqldb:file:<file>");
    dataSource.setUsername("");
    dataSource.setPassword("");
    return dataSource;

What I don't know is how to match the jdbc:initialize data part in my Java setup.

like image 315
user16655 Avatar asked Oct 18 '22 07:10

user16655


1 Answers

@Bean
public DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).addScript("classpath:scripts/create-table-if-not-exists").build();
}

http://docs.spring.io/spring-framework/docs/3.0.0.RC3/reference/html/ch12s08.html

like image 182
coffeenjava Avatar answered Oct 27 '22 16:10

coffeenjava