Java h2(嵌入式模式)数据库文件问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2442867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:43:43  来源:igfitidea点击:

h2 (embedded mode ) database files problem

javah2

提问by aeter

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

我的 src 目录(Java、Eclipse)中有一个 h2-database 文件: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).

  • 从命令行启动 h2.jar(以及端口 8082 上的 h2 浏览器界面),我在 h2test.db 中创建了 2 个表,“test1”和“test2”,并在其中放入了一些数据;

  • 当尝试从 Java 代码 (JDBC) 访问它们时,它会抛出“找不到表异常”。java 代码中的“显示表”显示了一个具有 0 行的结果集。

  • 另外,当从java代码(CREATE TABLE ...等)创建一个新表('newtest')时,我在之后启动h2.jar浏览器界面时看不到它;只显示了另外两个表('test1' 和 'test2')(但是新创建的表 'newtest' 可以从 java 代码访问)。

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?

我对嵌入式数据库缺乏经验;我相信我在这里做了一些根本错误的事情。我的假设是,我正在访问同一个文件 - 一次来自 java 应用程序,一次来自 h2 控制台浏览器界面。我似乎无法理解它,我在这里做错了什么?

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.

这个结果集有 0 行(没有表格),而不是向我展示 2 个表格。

H2 Console-browser interface settings:

H2 Console-浏览器界面设置:

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.

EDIT2:我将数据库复制到一个新文件夹。现在,新文件夹中的 db 文件与 'newtest' 表(来自 java 代码)以及 'test1' 和 'test2' 表(来自控制台浏览器 h2 界面)一起显示 - 与旧数据库完全相同文件显示。因此问题仍然存在于 db 文件的副本中。

回答by trashgod

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=TRUEto avoid creating spurious database files.

为方便起见,追加;IFEXISTS=TRUE以避免创建虚假的数据库文件。

See Connecting to a Database using JDBCfor more.

有关更多信息,请参阅使用 JDBC 连接到数据库

H2 ServerURLs are relative to the -baseDirspecified as a parameter to main().

H2服务器URL 相对于-baseDir指定为 的参数main()

回答by shms

Also you can like this

你也可以喜欢这个

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

then java looks db folder from project folder

然后java从项目文件夹中查找db文件夹

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

回答by Aconic

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

如果您正在使用 Hibernate,请在 hibernate.cfg.xml 文件中尝试此操作:

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

without *.db extension at the end

最后没有 *.db 扩展名

回答by voho

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.

如果您在 JDBC url 中使用一些特殊参数,也会出现问题,数据库文件名可能因各种情况而异。

In my case, I had two URLs:

就我而言,我有两个 URL:

  • jdbc:h2:~/XXX;MVCC=FALSE;MV_STORE=FALSE
  • jdbc:h2:~/XXX
  • 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.

第一个案例创建了 XXX.h2.db 文件,第二个是 XXX.mv.db,小心。