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

发布于 2024-08-25 06:06:06 字数 1251 浏览 7 评论 0原文

我的 src 目录(Java、Eclipse)中有一个 h2 数据库文件:h2test.db

问题:

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

  • 当尝试从 java 代码 (JDBC) 访问它们时,它会抛出“未找到表异常”。 java 代码中的“显示表”显示包含 0 行的结果集。

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

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

编辑:根据要求,添加一些代码:

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 个表。

H2 控制台浏览器界面设置:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琴流音 2024-09-01 06:06:06

对于嵌入模式,您需要检查路径。例如,使用相对于主目录的路径:

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

为确保

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

方便,请附加 ;IFEXISTS=TRUE 以避免创建虚假数据库文件。

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

H2 服务器 URL 相对于 -baseDir< /code> 指定为 main() 的参数。

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

夏末的微笑 2024-09-01 06:06:06

如果您在 JDBC url 中使用一些特殊参数,也可能会出现问题,不同情况下数据库文件名可能会有所不同。

就我而言,我有两个 URL:

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

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

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.

蓝礼 2024-09-01 06:06:06

你也可以像这样

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

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

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

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
-->....
美人迟暮 2024-09-01 06:06:06

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

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

末尾不带 *.db 扩展名

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文