English 中文(简体)
h2 (embedded mode ) database files problem
原标题:
  • 时间:2010-03-14 16:40:11
  •  标签:
  • 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.

问题回答

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().

Also there can be a problem if you use some special parameters in your JDBC url, the database file name can differ for various cases.

In my case, I had two URLs:

  • jdbc:h2:~/XXX;MVCC=FALSE;MV_STORE=FALSE
  • jdbc:h2:~/XXX

This first case created XXX.h2.db file, the second one XXX.mv.db, beware.

Also you can like this

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

then java looks db folder from project folder

->projectName // project folder
-->src        // src folder
-->db         // here your database folder
-->....

If you are using Hibernate try this in hibernate.cfg.xml file:

<property name="connection.url">jdbc:h2:file:db/h2test</property>

without *.db extension at the end





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签