Visual H2 Database Action in Eclipse

H2 is a popular database for development, but since it is usually embedded in application memory visually inspecting application data can be difficult. H2 can be configured to be saved to disk, but even when stored in file format it is not readily obvious how to view H2 application data. We're going to cover two approaches to going Visual with H2 in Eclipse.

The Eclipse Data Source Explorer

Eclipse has a pretty useful Data Source Explorer you can use for H2 and other databases. Let's start by opening the Data Source Explorer View.

In our Data Source Explorer we need to create a new Connection Profile.

We create a Connection Profile and select a driver for our H2 database. The JDBC Generic Connection will do the job for us.

Now we need an H2 driver for our Generic JDBC Connector, so if you're like me you've got a local directory with a bunch of handy Java JAR files in it, which may include a version of H2.JAR. Begin by clicking on the New Driver Definition button.

Now we add the H2 Jar from that handy local JAR directory I mentioned.

We need to provide a few properties. Don't worry about the URL of the database. This is overridden in your Eclipse Data Source Explorer Connection Profile.

The only field here that's important is the Driver Class which has an interesting function. It contains a popup button shown on mouse-over of the field that brings up a nifty search dialog to retrieve the class name from the JAR for you, in this case org.h2.Driver. This is what it returned from our H2.jar file search.

Okay, we've got Visual H2 in our Eclipse Data Source Explorer View!

Let's look at the connection properties, which we can change at any time. Convention dictates we create our H2 databases in our application root directory, but I recommend storing them off your Home directory. The JDBC URL doesn't like absolute paths and you can get in trouble using the ./ for application root. Trust the Tilde.

Our result is pretty sweet, eh? Eclipse gives us a number of database tools we can use with H2.

Another Approach for going Visual H2 in Eclipse

Now for something completely different. We can ignore the Eclipse Data Source Explorer altogether and use the H2Console from our H2.JAR to view our H2 data in a browser. The command is something like this.

java -cp /ubuntuland/java/jars/h2-1.4.187.jar org.h2.tools.Server -web

I created an alias in my .bashrc (Don't you love Linux) so all I have to do is type “H2” and I'm good to go.

With the Web Console is up and running we can go to our Eclipse Browser View (or externally) to take a quick look around.

This may be a more flexible approach depending on how often you want to look at your H2 data and the number of H2 databases you're working with.

Now that we know how to go Visual H2, let me leave you with this final thought. If you need to view your application data a lot, don't use H2.

Posted February 19, 2016 04:09 PM EST

More Like This Post