以编程方式启动H2数据库 [英] Start H2 database programmatically

查看:151
本文介绍了以编程方式启动H2数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java编写服务器 - 客户端应用程序,我需要在服务器端实现本地数据库,我决定使用H2数据库引擎。

I'm coding a server-client application in Java and I need to implement a local database on the server side and I decided to go for H2 database engine.

还有一件事是我通过TCP连接来启动和运行数据库。
这是我到目前为止所说的:

One more thing to add is that I usa TCP connection to start and run the database. This is what I 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);   

连接字符串是 jdbc:h2:tcp:// localhost / 〜/ test

这段代码返回异常:

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

我跟着这篇文章

推荐答案

这样的事情应该有效

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();

输出为Connection Established:H2 / STACKOVERFLOW

and the output is Connection Established: H2/STACKOVERFLOW

已经使用h2-1.4.184

This has been tested with h2-1.4.184

这篇关于以编程方式启动H2数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆