Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

h2 (embedded mode ) database files problem

Tags:

java

h2

There is a h2-database file in my src directory (Java, Eclipse): h2test.db

The problem:

  • starting the h2.jar from the command line (and thus the h2 browser interface on port 8082), I have created 2 tables, 'test1' and 'test2' in h2test.db and I have put some data in them;

  • when trying to access them from java code (JDBC), it throws me "table not found exception". A "show tables" from the java code shows a resultset with 0 rows.

  • Also, when creating a new table ('newtest') from the java code (CREATE TABLE ... etc), I cannot see it when starting the h2.jar browser interface afterwards; just the other two tables ('test1' and 'test2') are shown (but then the newly created table 'newtest' is accessible from the java code).

I'm inexperienced with embedded databases; I believe I'm doing something fundamentally wrong here. My assumption is, that I'm accessing the same file - once from the java app, and once from the h2 console-browser interface. I cannot seem to understand it, what am I doing wrong here?

EDIT: as requested, adding some code:

Java code:

Class.forName("org.h2.Driver");
String url = "jdbc:h2:" + "db/h2test.db";
String user = "aeter"; 
String password = "aeter"; 
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps2 = conn.prepareStatement("Show tables;");
ResultSet rs = ps2.executeQuery();

This resultset has 0 rows (no tables), instead of showing me the 2 tables.

H2 Console-browser interface settings:

Settings: Generic h2(embedded)
driver class: org.h2.Driver
JDBC URL: jdbc:h2:../../workspace/project_name/src/db/h2test.db
user name: aeter
password: aeter 

EDIT2: I copied the database to a new folder. Now the db file in the new folder is shown with the 'newtest' table (from the java code) and with the 'test1' and 'test2' tables (from the console-browser h2 interface) - exactly the same way the older db file was shown. So the problem persists with the copy of the db file.

like image 313
aeter Avatar asked Mar 14 '10 16:03

aeter


People also ask

How do I access H2 DB files?

The easiest way to access the console is to double click the H2 database jar file at <installation-directory>\confluence\WEB-INF\lib\h2-x.x.x.jar .

Is H2 embedded database?

H2 is an embedded, open-source, and in-memory database. It is a relational database management system written in Java.

Is H2 database reliable?

H2 is a terrific database engine. After we solved a problem not related to H2, we switched from PostgreSQL and MySQL (both still supported) to H2. H2 became our primary database choice.


1 Answers

For embedded mode, you'll need to check the path. For example, use a path relative to your home directory:

"jdbc:h2:file:~/db/h2test.db"

To be sure, use a full path:

"jdbc:h2:file:/users/aeter/db/h2test.db"

For convenience, append ;IFEXISTS=TRUE to avoid creating spurious database files.

See Connecting to a Database using JDBC for more.

H2 Server URLs are relative to the -baseDir specified as a parameter to main().

like image 76
trashgod Avatar answered Sep 27 '22 22:09

trashgod