2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

how to connect with hsqldb using spring jdbctemplate and bean?

how to connect with hsqldb using spring jdbctemplate and bean?
userimage

You can download the db driver of use the below dependency into your application.


<!-- HyperSQL DB -->
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>${hsqldb.version}</version>
        </dependency>


Include below script into your spring xml,


    <jdbc:embedded-database id="dataSource" type="HSQL">
        <jdbc:script location="classpath:db/sql/create-db.sql" />
        <jdbc:script location="classpath:db/sql/insert-data.sql" />
    </jdbc:embedded-database>


Java code to use the above embeded datasource into your spring program,


import javax.sql.DataSource;

import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
//...

    @Bean
    public DataSource dataSource() {
       
        // no need shutdown, EmbeddedDatabaseFactoryBean will take care of this
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        EmbeddedDatabase db = builder
            .setType(EmbeddedDatabaseType.HSQL) //.H2 or .DERBY
            .addScript("db/sql/create-db.sql")
            .addScript("db/sql/insert-data.sql")
            .build();
        return db;
    }



Answer is