On H2 documentation, it says only the web server supports browser connections. Does this mean we can only access the H2 Database by console in WebServer mode, not TcpServer? But when I do below test, the result is totally not as expected.
public class TestMem {
public static void main(String... args) throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:test");
conn.createStatement().execute("create table test(id int)");
Server server = Server.createTcpServer().start();//1.TcpServer
// Server server = Server.createWebServer().start();//2.WebServer
System.out.println("Server started and connection is open.");
System.out.println("URL: jdbc:h2:" + server.getURL() + "/mem:test");
Thread.sleep(5*60*1000);
System.out.println("Stopping server and closing the connection");
server.stop();
conn.close();
}
}
If I start a TcpServer, I can visit the DB by this url: jdbc:h2:tcp://localhost:9092/mem:test in console.
//Use TcpServer
Server server = Server.createTcpServer().start();
But when I start a WebServer, I tried to connect use jdbc:h2:http ://localhost:8082/mem:test, below exception will throw: IO Exception: "java.io.IOException: The filename, directory name, or volume label syntax is incorrect"; "http ://localhost:8082/mem:test.h2.db" [90031-172] 90031/90031 (Help).
//Use WebServer
Server server = Server.createWebServer().start();
Here are my questions
Accessing the H2 Console H2 database has an embedded GUI console for browsing the contents of a database and running SQL queries. By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page.
Before accessing the H2 database, we must enable it by using the following property. Once we have enabled the H2 console, now we can access the H2 console in the browser by invoking the URL http://localhost:8082/h2-console.
Java Connect to H2 Database in Server Modejdbc:h2:tcp://localhost/~/test connect to the 'test' database in the user home directory on the server (local computer). jdbc:h2:tcp://192.168.1.3:9092 //data/test connect to the 'test' database in the /data directory on the remote server.
The H2 Console tool (created using Server.createWebServer
) is a web server and a small web application that allows you to connect to a database (any JDBC database) using a web browser (such as Firefox, Google Chrome, Internet Explorer, and so on).
The H2 TCP Server (created using Server.createTcpServer
) is a server for H2 JDBC clients. It is not a web server and not a web application. You can not connect to this server from a web browser.
If you start both the Console tool (web server) and the TCP server, then you can connect using a browser to the Console tool, which can then connect to a H2 database in server mode.
About database URLs: see the documentation about database URLs.
Some additional comment on the answer of Thomas Mueller:
"The H2 Console tool ... a small web application"
You can see it as a "small alternative" to database applications like DBeaver or Navicat.
In addition to that, if you start the H2 console from within the same JVM as your H2 database (which has for example been started with org.h2.Tools.RunScript.execute(...)), you will be able to use the "embedded mode" of the H2 Console tool to connect to your H2 database without using a H2 TCP server.
(Other database tools like DBeaver will not run in the same JVM. If you use the embedded H2 mode with DBeaver, you will be connected to an extra temporary database inside the JVM of DBeaver.)
Example for embedded mode
If you started a H2 database "test_database" on port 8085 from within the same JVM, with the option DB_CLOSE_DELAY=-1 and without also starting a TCP server ... start a web browser and enter following address to show the H2 Console tool web application:
http://localhost:8085
Use for example following settings to connect in embedded mode:
Setting Name: Generic H2 (Embedded)
Driver Class: org.h2.Driver
JDBC URL: jdbc:h2:mem:test_database;MODE=MySQL
UserName: {user, e.g. root}
Password: {password, e.g mypassword}
Example for server mode
If you started a TCP server you can connect to the H2 database in server mode. You can do this with
Start a web browser and enter for example following address to show the H2 Console tool web application:
http://localhost:8085
Use for example following settings to connect in server mode:
Setting Name: Generic H2 (Server)
Driver Class: org.h2.Driver
JDBC URL: jdbc:h2:tcp://localhost:9095/mem:test_database;MODE=MySQL
UserName: {user, e.g. root}
Password: {password, e.g mypassword}
You can also use the url
jdbc:h2:tcp://localhost:9095/mem:test_database;MODE=MySQL
to connect for example with DBeaver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With