Configure H2 DB with Spring Boot

Prerequisite

Below tools should be installed on your machine

  1. Java
  2. Maven
  3. STS/Eclipse

GO to https://start.spring.io/

Add dependency as

  1. H2 Database 
  2. Spring Web
  3. Spring Data JPA

NOTE: Kindly select java version according to your project need ..I am using java 1.8 else you will get below exception when you run mvn clean install

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven: Compilation failure: Compilation failure

Dependencies will look like below

Kindly add required dependencies as shown in above screenshot and down load project by clicking on “GENERATE” button. ZIP will get downloaded.

NOTE: if you are downloading on office laptop then you might need to unblock zip before unzipping. for doing this right click on zip file and go to properties and unblock it. so you will able to unzip it completely.

After unzip you will see folder generate like below screenshot

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
	
		
	</properties>
	<dependencies>
	<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
			<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Go to your editor and import project.

For eclipse or STS Users..

1 GO to file

2 click on import

3 type maven and select Existing maven project

4 It will show pop up to select folder of your project. Kindly select folder which you unzipped.

5 Select pom.xml and click on Finish button

6. Now your project will get created in workspace and all dependencies will get downloaded by maven.Make sure maven is installed on your machine.

In below screenshot and in highlighted part you can see its showing “boot” project in bracket because spring tool suit will automatically detect it as spring project. For this in eclipse you need to download plugins.

Your project will be visible in workspace now

7. Right click on project and go to run and do maven install as shown in below screen shot

On console you will see below logs

8. Once build is successful then add below code in DemoApplication.java and add below configurations to application.properties file as well

DemoApplication.java

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
		
	System.out.println("Welcome to spring boot");
	}
}

application.properties

spring.datasource.url=jdbc:h2:file:C:/data/demo
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
server.port=9000
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
spring.jpa.hibernate.ddl-auto=none

Above code will have datasource related to H2 DB and it will tell spring boot on which path database whould be accessible in browser. Your application will run on 8888 port

Add below files in resources folder

Add data.sql file as shown below

data.sql

DROP TABLE IF EXISTS employee;
 
CREATE TABLE employee (
  id INT AUTO_INCREMENT  PRIMARY KEY,
  first_name VARCHAR(250) NOT NULL,
  last_name VARCHAR(250) NOT NULL
  
);
 
INSERT INTO employee (first_name, last_name) VALUES
  ('James', 'mill')

schema.sql

DROP TABLE IF EXISTS NATION;
CREATE TABLE NATION (
    id   INTEGER      NOT NULL AUTO_INCREMENT,
    name VARCHAR(128) NOT NULL,
    PRIMARY KEY (id)
);

9. Now Run application as spring boot app shown in below screenshot

10. Congrats now your application is running successfully and your H2 Db is running

11. Now go to http://localhost:9000/h2-console

you will see below screen and please enter jdbc url and pasword used in applicaiton.properties file

Enter password and click on connect and you will able to see DB console

Now you can able to use this DB for configuration with JPA library or any other purpose

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: