Reputation: 23
Currently have a simple Java Program that creates an H2 Database like so:
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:
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?
Upvotes: 0
Views: 2314
Reputation: 126
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.
Upvotes: 3