o7planning

Install H2 Database and Use H2 Console

  1. Download H2
  2. Install and run H2
  3. What is H2 Console?
  4. H2 Console & H2 (Server)
  5. H2 Console & H2 (Embedded)
  6. Connect to a database other than the H2

1. Download H2

To download H2 database, you visit the following address:
The simplest way is downloading ZIP file, which is suitable for all different operating systems and doesn't need to be installed. You only need to extract it in a folder, you can use it.
OK, Now, I download the ZIP file.

2. Install and run H2

The H2 is a database entirely written by Java. Therefore, to run it , you must ensure that your computer has Java installed. If not, you can refer to how to install Java in the following posts:
After downloading the ZIP file in the above step, extract it in a folder.
If you use Windows operating system, you need to run only h2.bat file by double click on it.
For other operating systems such as Linux, Mac OS,.. you need to run the h2.sh file:
Grant the permission for the h2.sh to be able execute by performing the following command:
chmod +x /home/tran/Database/h2/bin/h2.sh
Or use an interface to grant permission.
Open the Terminal window to run the h2.sh file:
The H2 database run and a "H2 Console" window are opened on the browser:

3. What is H2 Console?

The H2 Console is a visual tool helping you administer the H2 database. In fact, the H2 Console can be used to administer most other relational databases (Oracle, SQL Server, MySQL, etc.).
The H2 Console allows you to query and change data visually.

4. H2 Console & H2 (Server)

Create a H2 database with the H2 (Server) type. This database will store its data in hard drive
  • Saved Settings: Generic H2 (Server)
  • Setting Name: Generic H2 (Server)
  • Driver Class: org.h2.Driver
  • JDBC URL: jdbc:h2:tcp://localhost/~/test
  • User Name: sa
  • Password:
After pressing "Connect", 2 files will be created in your hard drive and put in the the folder:
  • Windows: C:/users/{user}
  • Linux: /home/{user}

5. H2 Console & H2 (Embedded)

You can create a database with the H2 (Embedded) type. This database will store its data in the computer's memory, which means if you turn off the H2 (or shut down the computer ), all data will be lost. However, since the data is stored in the memory, data access is very fast. This kind of database is often embedded into an application to exclusively use for this application and not shared with other applications. When the application is shut down, this database will also be disabled.
Different from the H2 (Server), the H2 (Embedded) will not create any files in the hard drive.
  • Saved Settings: Generic H2 (Server)
  • Setting Name: Generic H2 (Embedded)
  • Driver Class: org.h2.Driver
  • JDBC URL: jdbc:h2:mem:testdb
  • User Name: sa
  • Password:

6. Connect to a database other than the H2

Using the H2 Console enable you to connect to other databases such as Oracle, MySQL, .... In this case, you need to download additional JDBC Driver libraries.
For example, for MySQL, Copy the JDBC Driver (mysql-connector-***.jar) library ad put it into the bin folder of the H2.
Edit the contents of h2.bat and h2.sh files:
h2.bat
@java -cp "h2-1.4.197.jar;mysql-connector-java-6.0.6.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Console %*
@if errorlevel 1 pause
h2.sh
#!/bin/sh
dir=$(dirname "$0")
java -cp "$dir/h2-1.4.197.jar:mysql-connector-java-6.0.6.jar:$H2DRIVERS:$CLASSPATH" org.h2.tools.Console "$@"