h2(嵌入模式)数据库文件问题 [英] h2 (embedded mode ) database files problem
问题描述
我的src目录中有一个h2-database文件(Java,Eclipse):h2test.db
There is a h2-database file in my src directory (Java, Eclipse): h2test.db
问题:
-
从命令行启动h2.jar(以及端口8082上的h2浏览器界面),我创建了2个表,'test1'和'test2'在h2test.db中我已经放了一些数据;
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;
当试图从java代码(JDBC)访问它们时,它会抛出找不到表例外。 java代码中的show tables显示了一个0行的结果集。
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.
此外,从java代码(CREATE TABLE ...等)创建新表('newtest')时,我在启动时无法看到它之后的h2.jar浏览器界面;只显示了其他两个表('test1'和'test2')(但随后可以从java代码中访问新创建的表'newtest')。
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).
我对嵌入式数据库缺乏经验;我相信我在这里做了一些根本错误的事情。我的假设是,我正在访问同一个文件 - 一次来自java应用程序,一次来自h2控制台 - 浏览器界面。我似乎无法理解,我在这里做错了什么?
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?
编辑:根据要求,添加一些代码:
as requested, adding some code:
Java代码:
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();
此结果集有0行(无表),而不是给我看了2个表。
This resultset has 0 rows (no tables), instead of showing me the 2 tables.
H2控制台 - 浏览器界面设置:
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:我将数据库复制到新文件夹。现在,新文件夹中的db文件显示为'newtest'表(来自java代码),并带有'test1'和'test2'表(来自console-browser h2接口) - 与旧数据库完全相同文件显示。因此,db文件的副本仍然存在问题。
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"
为方便起见,附加; IFEXISTS = TRUE
以避免创建虚假的数据库文件。
For convenience, append ;IFEXISTS=TRUE
to avoid creating spurious database files.
请参阅 使用JDBC连接数据库 更多。
See Connecting to a Database using JDBC for more.
H2 服务器 URL相对于 -baseDir
指定为 main()
的参数。
H2 Server URLs are relative to the -baseDir
specified as a parameter to main()
.
这篇关于h2(嵌入模式)数据库文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!