I’m a swiss Master student in Computer Science. I’m very interested in C++, open source projects, Linux, Intel Assembly and Agile. I'm currently working on Eddi, a new programming language that I created to improve my skills in C++. I've also worked a lot on Java technologies (Sprint, Osgi, Play!, ...), but I'm not currently working with Java anymore. Baptiste is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Presentation and Use of H2 Database Engine

  • submit to reddit

It's a long time since I started to using the H2 Database Engine as an embedded database in JTheque and other projects. This post is a presentation of this database engine and some information about its utilization.

H2 is a pure Java database. It can work as an embedded database or in server mode. The developers of the database have ensured there is a very small footprint for this database; the jar file is just around 1MB.


Another feature the developers find important in this database is the performance. Here are two comparison graphs for H2, Derby and HSQLDB, generated directly from the benchmark data from the official H2 site.

Performances of H2 in Embedded Mode

Performances of H2 in Embedded Mode

Performances of H2 in Server Mode

Performances of H2 in Server Mode

As you can see, the performance of H2 is very good. It’s the main reason I’ve used H2 instead of HSQLDB.


H2 can work in three modes. Embedded, Server or Mixed modes. In each mode, you can use memory or persistent tables.

H2 Embedded Mode

In this mode, the database is only accessible from the current virtual machine. It’s the fastest connection mode.

H2 Server Mode

In this mode, the database runs in a server, so several applications can access the database engines. Of course, this mode is slower than in embedded mode, because all data is transfered using TCP/IP.

H2 Mixed Mode

H2 Mixed Mode

In this mode, the first application creates the database in embedded mode but also starts a server to provide access to the database.


The use of this database is as simple as the use of any other database. You just have to create the database using the JDBC Driver and the database will be automatically created, or the server launched, if needed.

Here is an example to create an database in embedded mode, you just have to do the following :

Connection connexion = DriverManager.getConnection("jdbc:h2:test", "sa", "");

test is the name of the database. If the database already exists, the data will be loaded.

You can also create a database in memory. But when you close the database, there is no data persisted. It’s useful for benchmarking by example. It’s also the mode with the best performance. Here is an example showing how to create a memory database (named test) :

Connection connexion = DriverManager.getConnection("jdbc:h2:mem:test", "sa",  "");

To start the server, you have two choices. You can launch it from the command line :

java -cp h2*.jar org.h2.tools.Server

Or programmatically :

Server server = Server.createTcpServer(args).start();

And to connect to a remote server, just change the JDBC URL :

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

The database is closed when the last connection to the database is closed.

The SQL used is strandard SQL with some adds to support the features of H2. The syntax is described on the oficial site : SQL Grammar. By example, here are the SQL requests for creating the different types of database of H2 :

  • A cached table is the default type of table. The data is persistent and not limited by the memory.
  • A memory table is also persistent but the index of data is kept in the memory, so a memory table cannot be too large.
  • A temporary table is deleted when closing the database.

So, I think we covered here the main things we must know to start using H2 Database Engine.

For more information, consult the official site.

From http://www.baptiste-wicht.com/2010/08/presentation-usage-h2-database-engine/

Published at DZone with permission of Baptiste Wicht, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Tomasz Nurkiewicz replied on Wed, 2010/08/11 - 1:51am

One of the coolest features I find while development is so called Automatic Mixed Mode, that starts the database when the first connection to it is made and shuts it down when the last connection is closed. In this mode you don't have to start the server manually, but the server seems to be always available, no matter how you connect (SquirrelSQL, your application, etc.)

Martin Grigorov replied on Thu, 2010/08/12 - 11:07am

Class.forName("org.hsqldb.jdbcDriver").newInstance(); looks like a typo.

Thomas Mueller replied on Fri, 2010/08/13 - 2:54pm

Class.forName("org.h2.Driver") is the correct way to load the H2 driver. Actually with Java 6 even this is not needed because the JDBC driver is automatically loaded if the h2*.jar file is in the classpath.

The database URL jdbc:h2:test is correct, but you need to be aware it will create a database file in the current working directory, meaning in the directory where the application was started. If you start the application in another directory later on, a new database is created. To avoid such problems, you can use an absolute directory such as jdbc:h2:/data/test or a directory relative to the current user home directory: jdbc:h2:~/test.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.