Search code examples
springjdbccrudderbyin-memory-database

Any way to create DB(derby) in Spring MVC Project?


I am working on a program which uses memory DB(in my case, it is apache derby, is included in jdk 1.6 and further). I can set resources like dataSource and sqlSession bean to 'root-context.xml', but do not know how to create DB and table.

If table and DB are already exist, I can do CRUD via usual way. However, this time I should make a DB and new Table on first time. I already tried Maker Class and put 'init-method' to create DB and table before was is uploaded, but it does not work.

How can I make a Spring was program which makes Memory DB and Table before running?

P.S. I really like to use CRUD in Spring way like SqlSessionTemplate or annotation. But almost of derby sample uses PreparedStatement or Statement. If you have any good sample links what I am looking for, share please. Thanks :D


Solution

  • You need to use the jdbc namespace which includes an embedded-database tag with support for Derby out of the box. The tag takes nested script tags to define scripts to run (they are run in the order declared).

    <jdbc:embedded-database id="myDB" type="DERBY" >
        <jdbc:script location="classpath:sql/schema.sql"/>
        <jdbc:script location="classpath:sql/data.sql"/>
    </jdbc:embedded-database>