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>
Add below dependency also under <dependencies>below dependency </dependencies>
Create table in H2 database
- 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:
- Take insert component and give query as: insert into student (roll, name, marks) values (1, ‘shweta’, ’80’);
- After that take a logger.
- So flow will look like this
Image: Flow for inserting value into database
- Now Run the application. Check logger values printed in console. Values will be inserted in the table.
Select Data from Table:
- Take select component.In select give query as select * from student;
- After the ‘select’ component, convert data in json in the transform message .
- Run the application
Image : Flow for selecting values from Database
- Check console now
Image: Values from table
That’s all for now!