Fully Working Prototypes With Spring Boot and H2

DZone 's Guide to

Fully Working Prototypes With Spring Boot and H2

In-memory databases are great for unit tests, but they can also help with prototyping! We take a look at how to do just that using Spring Boot and H2.

· Java Zone ·
Free Resource

We use a lot of H2 with Spring, especially for unit tests. However, instead of unit tests, we might want to have a fully functional prototype with data to display.

H2 is the perfect candidate for that. It works great with Spring, has strong syntax compatibility with most databases out there, and provides a UI to check your data.

Imagine the scenario of an interview assignment. You want your example to work out of the box, with as little configuration as possible for the reviewer.

The plan is to have the application up and running with data. Before accessing the application, we might as well add data to it. Then, we need to have a proper way to display the data added without adding extra code.

The first step is to go to the Spring initializr and add the Web and H2 dependencies. Also, we shall add the JDBC property.

The end result will give us a build.gradle file like this:

buildscript {
ext {
springBootVersion = '2.0.6.RELEASE'
repositories {
dependencies {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.gkatzioura.springbooth2'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {

dependencies {

Since we added the JDBC property, we can have some schema scripts executed once the application is started.
Thus, we need to create a schema.sql file containing the SQL statements which create the schema.

INSERT INTO application_user (ID,USER_NAME, PASSWORD) values (1,'test','password-hash');

The next step is to enable the H2 console. We will go with the YAML approach; however, you can do it either using a properties file or environmental variables.

      enabled: true

Now, once we get our Spring application running, we can navigate at the http://localhost:8080/h2-console endpoint.
We shall be presented with the default credentials needed.

Once we have logged in, we can query for the user we had inserted on our startup SQL script.

That's it! This can make wonders for prototypes, interview assignments, and blog posts like this!

h2, in-memory database, java, prototyping, spring boot, tutorial

Published at DZone with permission of Emmanouil Gkatziouras , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}