Java – start H2 database programmatically

I am writing a server - client application in Java. I need to implement a local database on the server. I decided to use H2 database engine

Another thing is that I start and run the database through a TCP connection This is what I have put together so far:

Class.forName("org.h2.Driver");  
Server server = Server.createTcpServer(DB_PATH).start();

Connection currentConn = DriverManager.getConnection(DB_PATH,DB_USER,DB_PASSWORD);

The connection string is JDBC: H2: TCP: / / localhost / ~ / test

This code returns an exception:

Feature not supported: "jdbc:h2:tcp://localhost/~/test" [50100-176]

I followed this article

Solution

Such things should work

Server server = null;
            try {
                server = Server.createTcpServer("-tcpAllowOthers").start();
                Class.forName("org.h2.Driver");
                Connection conn = DriverManager.
                    getConnection("jdbc:h2:tcp://localhost/~/stackoverflow","sa","");
                System.out.println("Connection Established: "
                        + conn.getMetaData().getDatabaseProductName() + "/" + conn.getCatalog());

            } catch (Exception e) {
                e.printStackTrace();

The output is connection established: H2 / stockoverflow

This has passed h2-1.4 184 test

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>