H2 Database + Mule

Author: Shweta A

H2 is an open-source lightweight Java database. Mainly, H2 database can be configured to run as in memory database, which means that data will not persist on the disk. Because of embedded databases it is not used for production development, but mostly used for development and testing.

Features of H2 Database

  • It is an extremely fast database engine
  • H2 is open source and written in Java
  • H2 supports clustering and multi-version concurrency
  • It has strong security features

Let’s configure the H2 database in our mule flow

  • Create  a mule project. After creating mule project we have to add dependencies for the H2 database in pom.xml manually

Add the below dependencies under <plugins> <configuration>below dependency</configuration> </plugins>

                   <configuration>

<sharedLibraries>

                                     <sharedLibrary>

                                               <groupId>com.h2database</groupId>

                                                        <artifactId>h2</artifactId>

                                     </sharedLibrary>

                            </sharedLibraries>

                   <configuration>

Add below dependency also under <dependencies>below dependency </dependencies>

<dependency>

                    <groupId>com.h2database</groupId>

                    <artifactId>h2</artifactId>

                    <version>1.4.200</version>

         </dependency>

Create table in H2 database

  1. In mule project take one listener then take one Execute DDL component for database.

Image: create table in H2 database

Configuration of Database connector:

  • Select Generic connection in connection type.
  • Driver class name as org.h2.Driver
  • For url, create one folder in your computer.Now in the URL section give value as ‘jdbc:h2:file:’ After this value give the path name of the folder created. So now for my mule flow URL will be jdbc:h2:file:D:/h2 Db/h2Test Here ‘D:/h2 Db’ is the path of my folder created and ‘h2Test’ is the name of Database. Name of the database is user selected. You can give any name for the database .
  • After this Test connection.

Image:configuration for H2 database

  • In Execute DDL component give the query for creating table as :

CREATE TABLE student (

   roll INT  ,

  name VARCHAR(50) ,

  marks VARCHAR(50) )

  • Take one logger and now run the application.
  • Now check in folder created, database will be created with name h2Test.mv.db

Insert Data into Database:

  1. Take insert component and give query as: insert into student (roll, name, marks) values (1, ‘shweta’, ’80’);
  2. After that take a logger.
  3. So flow will look like this

Image: Flow for inserting value into database

  1. Now Run the application. Check logger values printed in console. Values will be inserted in the table.

Select Data from Table:

  1. Take select component.In select give query as select * from student;
  2. After the ‘select’ component, convert data in json in the transform message .
  3. Run the application

Image : Flow for selecting values from Database

  1. Check console now

Image: Values from table

That’s all for now!

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.