Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - H2 Database


H2 is open source database written in Java. It is an in memory database, which means the data will be stored in memory and will not be saved in disk.


Well! We will be using H2 database for this tutorial, so that you get an understanding of how to deal with database.


But just remember, you cannot use H2 database in Production environment.


The next question that pops in our mind is, how to include H2 database in Spring Boot?


So, we need to add the below dependency,


<dependency>
	<groupId>com.h2database</groupId>
	<artifactId>h2</artifactId>
	<scope>runtime</scope>
</dependency>

In our 'pom.xml' file.


Example :




<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springbootproject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>
</project>



Spring Boot - H2 Database

Now, once we are done adding the dependency for H2 database, Spring Boot automatically configures the properties related to H2 database as stated below,


spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

This typically means, we can connect to the H2 database with the username 'sa' and empty password.


spring.datasource.username=sa
spring.datasource.password=

However, we can always change the above details by overriding the properties in 'application.properties' of Spring Boot.


Next, let us see that how can we view the H2 console from web browser.


To do that the only thing you need to follow the three step process,

  1. Go to the resources folder,


    Spring Boot - H2 Database

  2. Create the file named 'application.properties'.


    Spring Boot - H2 Database

  3. In 'application.properties' file, set the below property to true,



    spring.h2.console.enabled=true
    Spring Boot - H2 Database

And we are done.


Now start the Spring Boot application, go to the browser and type the below url,


http://localhost:8080/h2-console

Which will provide us the login page to connect to H2 Database.


Now, if we want to connect to the database, we need to create the database first and then connect it.


We would be seeing that in the coming tutorials.