如何在 Eclipse 的 Java 项目中创建 H2 数据库?

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

How do I create a H2 database inside a java project in Eclipse?

javaeclipseh2

提问by Erran Morad

I want to create an embedded H2 database in my simple java project in Eclipse. How do I do this programatically and package the db inside my code ? I tried a SO postfor this and got an error in my code.

我想在 Eclipse 的简单 java 项目中创建一个嵌入式 H2 数据库。如何以编程方式执行此操作并将数据库打包到我的代码中?我为此尝试了一个SO 帖子,但在我的代码中出现错误。

Code -

代码 -

public static void main(String[]args){

    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:?/test");
    ds.setUser("sa");
    ds.setPassword("sa");
    try {
        Connection conn = ds.getConnection();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

Error -

错误 -

org.h2.jdbc.JdbcSQLException: A file path that is implicitly relative to the 
current working directory is not allowed in the database URL "jdbc:h2:?/test". 
Use an absolute path, ~/name, ./name, or the baseDir setting instead. [90011-181]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.engine.ConnectionInfo.getName(ConnectionInfo.java:398)
    at org.h2.engine.Engine.openSession(Engine.java:45)
    at org.h2.engine.Engine.openSession(Engine.java:167)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:145)
    at org.h2.engine.Engine.createSession(Engine.java:128)
    at org.h2.engine.Engine.createSession(Engine.java:26)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:347)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:108)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:92)
    at org.h2.Driver.connect(Driver.java:72)
    at org.h2.jdbcx.JdbcDataSource.getJdbcConnection(JdbcDataSource.java:190)
    at org.h2.jdbcx.JdbcDataSource.getConnection(JdbcDataSource.java:161)
    at MyCode.main(MyCode.java:8)

I saw this link - https://groups.google.com/forum/#!msg/h2-database/SlSwte0DLSU/eWj0UaejdkEJand Where are my H2 database files?. Its not clear how I can get the exact path to test database on my windows pc.

我看到了这个链接 - https://groups.google.com/forum/#!msg/h2-database/SlSwte0DLSU/eWj0UaejdkEJ我的 H2 数据库文件在哪里?. 我不清楚如何在我的 Windows 电脑上获得测试数据库的确切路径。

How do I first access the test database and then create another database inside my java project ?

我如何首先访问测试数据库,然后在我的 java 项目中创建另一个数据库?

Thank you.

谢谢你。

回答by Thomas Mueller

You have used the wrong character. You need to use ~(tilde) and you have use ?(I don't know what it is, but it's not a tilde).

您使用了错误的字符。你需要使用~(波浪号)并且你已经使用了?(我不知道它是什么,但它不是波浪号)。

回答by kaqqao

The location of the H2 files is very nicely documented. To view the contents, execute the h2.jar. It is not only the driver, but also an executable that will start a web-based applications for DB management.

H2 文件的位置有很好的记录。要查看内容,请执行 h2.jar。它不仅是驱动程序,还是一个可执行文件,可以启动基于 Web 的应用程序进行数据库管理。