以编程方式嵌入 Java h2 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154636/
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-11 09:01:39  来源:igfitidea点击:

Embedding the Java h2 database programmatically

javadatabaseembeddingh2

提问by Georgi

At the moment we use HSQLDBas an embedded database, but we search for a database with less memory footprint as the data volume grows.

目前我们使用HSQLDB作为嵌入式数据库,但随着数据量的增长,我们搜索内存占用更少的数据库。

Derby / JavaDBis not an option at the moment because it stores properties globally in the system properties. So we thought of h2.

Derby / JavaDB目前不是一个选项,因为它在系统属性中全局存储属性。所以我们想到了h2

While we used HSQLDB we created a Server-object, set the parameters and started it. This is described here(and given as example in the class org.hsqldb.test.TestBase).

当我们使用 HSQLDB 时,我们创建了一个服务器对象,设置参数并启动它。这在此处进行了描述(并在类 org.hsqldb.test.TestBase 中作为示例给出)。

The question is: Can this be done analogous with the h2 database, too? Do you have any code samples for that? Scanning the h2-page, I did not find an example.

问题是:这也可以用 h2 数据库来完成吗?你有任何代码示例吗?扫描h2-page,没找到例子。

采纳答案by anjanb

From the download, I see that the file tutorial.html has this

从下载中,我看到文件tutorial.html有这个

import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();

回答by Alex Miller

Yes, you can run H2 in embedded mode. You just use the JDBC driver and connect to an embedded url like this (their example):

是的,您可以在嵌入式模式下运行 H2。您只需使用 JDBC 驱动程序并连接到这样的嵌入式 URL(他们的示例):

This database can be used in embedded mode, or in server mode. To use it in embedded mode, you need to:

* Add h2.jar to the classpath
* Use the JDBC driver class: org.h2.Driver
* The database URL jdbc:h2:~/test opens the database 'test' in your user home directory

该数据库可以在嵌入式模式下使用,也可以在服务器模式下使用。要在嵌入模式下使用它,您需要:

* Add h2.jar to the classpath
* Use the JDBC driver class: org.h2.Driver
* The database URL jdbc:h2:~/test opens the database 'test' in your user home directory

Example of connecting with JDBC to an embedded H2 database (adapted from http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcDataSource.html):

使用 JDBC 连接到嵌入式 H2 数据库的示例(改编自http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcDataSource.html):

import org.h2.jdbcx.JdbcDataSource;
// ...
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:?/test");
ds.setUser("sa");
ds.setPassword("sa");
Connection conn = ds.getConnection();

If you're looking to use H2 in a purely in-memory / embedded mode, you can do that too. See this link for more:

如果您希望在纯内存/嵌入式模式下使用 H2,您也可以这样做。查看此链接了解更多信息:

You just need to use a special URL in normal JDBC code like "jdbc:h2:mem:db1".

你只需要在普通的 JDBC 代码中使用一个特殊的 URL,比如“jdbc:h2:mem:db1”。

回答by javydreamercsw

If for some reason you need an embedded H2 database in server mode you can do it either manually using the API at http://www.h2database.com/javadoc/org/h2/tools/Server.html- or by appending ;AUTO_SERVER=TRUE to the database URL.

如果由于某种原因您需要在服务器模式下嵌入 H2 数据库,您可以使用http://www.h2database.com/javadoc/org/h2/tools/Server.html 上的 API 手动完成- 或通过附加 ;AUTO_SERVER =TRUE 到数据库 URL。