Thomas Scheiterbauer Thomas Scheiterbauer - 7 months ago 17
Java Question

Java Application with H2 Database

I have a java application which retrievs data from a H2 database.
The database and the application are installed on the same computer.
Now when i run the application under my Windows 10 pc everything works fine, but when i copy the java application onto my Raspberry Pi 2 B(Raspbian Jessie) it does not work anymore.
On both systems, I installed, the H2 database, which I can access via the browser interface, and also java.

The error on the Raspberry Pi is:

Feb 26, 2016 2:39:26 AM HelperPackage.DatabaseConnection startConnection
SEVERE: null
org.h2.jdbc.JdbcSQLException: Table "PRODUKTE" not found; SQL statement:
SELECT Name, Datum, Anzahl, Warnzeitpunkt, ID
FROM PRODUKTE
ORDER BY Datum;


You can see the whole error message here.

It looks like my table does not exist on my Raspberry Pi but when I take a look at the browser interface of my H2 database, the table is there.

My connection is created with the strings you can see in the .getConnection Method (import from java.sql.DriverManager).

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


Like I said, the application works fine on my Windows 10 pc.

Libraries I use:


  • pdfbox-app-2.0.0-RC3 for printing

  • h2-1.4.190 for the connection to the database


Answer

Soft-code explicit path

The expansion of the tilde (~) is not exactly standard.

Funny… I was concerned about this when I first saw that example in the H2 doc. I vaguely recall experiencing other situations where the tilde failed as a symbol for “my home directory/folder”, unrelated to H2 or JDBC.

For my own work, out of my vague paranoia, I have been using this soft-coded call instead of the tilde in my H2 JDBC URL:

System.getProperty( "user.home" )

Something like this.

String jdbcUrl = "jdbc:h2:" + System.getProperty( "user.home" ) + "/test" , "scott" , "tiger" );

See the Oracle Tutorial for more info on System and Properties.


This problem reminded me of a similar problem when I have seen situations where localhost failed to resolve because of some interplay between underlying libraries, forcing the explicit use of 127.0.0.1.

If you ever had time it might be interesting to dig through the H2 source to discover where exactly the tilde expansion failed on the Pi. Something about the Pi’s Java implementation? Or a library in the Pi’s Linux being called by Java?