Last Updated: February 25, 2016
·
2.179K
· kimble

Boostrapping H2 database with schema / data from SQL script

It's really easy to bootstrap H2 with schema and data stored in an SQL script on classpath. Here is an example with DBUtils.

import org.apache.commons.dbutils.QueryRunner;
import org.h2.jdbcx.JdbcConnectionPool;

// ....

JdbcConnectionPool connectionPool = JdbcConnectionPool.create("jdbc:h2:mem:testing", "user", "password");

QueryRunner runner = new QueryRunner(connectionPool);
runner.update("RUNSCRIPT FROM 'classpath:eventstore/jdbc/h2-bootstrap.sql' CHARSET 'utf-8'");

Pretty convenient for integration tests!