简体   繁体   English

在 H2 控制台中找不到在 JAVA 中创建的 H2 数据库?

[英]H2 Database created in JAVA not found in H2 Console?

Currently have a simple Java Program that creates an H2 Database like so:目前有一个简单的 Java 程序创建一个 H2 数据库,如下所示:

public static void main(String[] args) {
try {
    Connection conn = null;
    Statement stm = null;
    Class.forName("org.h2.Driver");
    conn = DriverManager.getConnection("jdbc:h2:D:/H2db/test", "sa", "sa");
    stm = conn.createStatement();
} catch (SQLException | ClassNotFoundException e) {
    e.printStackTrace();
}

I see the files created within the directory.我看到在目录中创建的文件。 Yet, when attempting to access this same Database from the H2 Console ( localhost:8082 ) and input both jdbc:h2:D:/H2db/test and jdbc:h2:D:\H2db\test , I get an error saying:然而,当尝试从 H2 控制台( localhost:8082 )访问同一个数据库并输入jdbc:h2:D:/H2db/testjdbc:h2:D:\H2db\test时,我收到一条错误消息:

Database `D:/H2db/test` not found, either pre-create it or 
allow remote database creation (not recommended in secure environments)

Anyone know why I cant access the Database through the console?有人知道为什么我不能通过控制台访问数据库吗?

That piece of code is just for connection to a previously created database.那段代码仅用于连接到以前创建的数据库。 To successfully connect to it you should create it first.要成功连接到它,您应该首先创建它。

$ java -cp bin/h2-1.4.199.jar org.h2.tools.Shell
Welcome to H2 Shell 1.4.199 (2019-03-13)
Exit with Ctrl+C
[Enter]   jdbc:h2:mem:test
URL       jdbc:h2:D:/h2db/test
[Enter]   org.h2.Driver
Driver    
[Enter]   sa
User      
Password

Then and only then you'll be able to connect to it through your java application.只有这样,您才能通过 java 应用程序连接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM