3 Java Embedded Databases

An embedded database is a database technology in which database management solutions are embedded into an application rather than provided as standalone database-system.

The embedded databases can be very helpful during development or testing phase, because they are lightweight, fast, improve testability and ease of configuration. We have plenty of choices for incorporating embedded databases. Be sure to apply pragmatism while picking one because each have their own pros and cons.

Screen Shot 2017-03-31 at 22.36.34

In this article, I will go over some of the embedded databases for working with Java.

HSQLDB (HyperSQL Database)

HyperSQL Database conforms to the SQL standard and JDBC specifications.

It supports all the core features that are expected from a modern relational database. It can be run either in embedded or server mode. HyperSQL database is built in pure Java and supports JDK 5, 6, 7 and 8 in HyperSQL 2.x.

Provides support for CallableStatement and PreparedStatement, including batch execution to speed up data processing.  With version 2.3.x onwards, it supports two-phased locking and multiversion concurrency control.

The package contains a jar file which contains the required components such as HyperSQL RDBMS Engine and the JDBC driver for embedding HyperSQL in a Java application.

Connection

Connection can be established as follows:

//load the JDBC driver

Class.forName("org.hsqldb.jdbcDriver" );

Connection connection = DriverManager.getConnection("jdbc:hsqldb:testdb", "sa", "");

Check the official documentation for more details.

H2

H2 is the Java SQL database which is open source and supports JDBC API. It supports both embedded and server modes. Provides support for row level locking and multi version concurrency.

Connection

Connection can be established as follows:

//load the driver

Class.forName("org.h2.Driver" );

Connection connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");

Here, ~/ indicates user home directory.

In their official documentation, the comparison of H2 with other database engines is quite comprehensive, which makes it easier to pick the one of your choice.

Apache Derby

Apache Derby is an open source relational database implemented entirely in Java and available under the Apache License, Version 2.0. It is based on Java, JDBC and SQL standards.

It supports client/server and embedded modes. All the standard features of a relational database are supported by Derby.

Connection

Use derby.jar in your application and connection can be established as follows:

//load the driver

Class.forName(“org.apache.derby.jdbc.EmbeddedDriver”);

Connection c = DriverManager.getConnection("jdbc:derby:testdb1;create=true”);

create=true creates the database if it does not exist and then connects to it. You don’t have to use this parameter unless you want the database to be specifically created if it doesn’t exist. Refer this link for more detailed explanation.

Also check the official documentation for more details.

Summary

The jar files provided by these database providers, contain all the necessary components for CRUD operations. Working with embedded databases is easy and fun. You just need to remember a few tips to make your application work with embedded databases successful!