我写了以下代码:
private static void startH2(){
Server server = null;
try {
server = Server.createTcpServer("-tcpAllowOthers").start();
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL", "sa", "");
} catch (Exception e) {
LOG.error("Error while initialize", e);
}
System.out.println("finish");
}
public static void main(String [] args){
startH2();
}
我运行我的主要方法并看到以下情况:
看起来Server.createTcpServer
创建了新的非守护线程。
但是通过 url localhost:8082
我看不到 h2 Web 控制台(实际结果 - ERR_CONNECTION_REFUSED
)
如何解决这个问题?
附注
我通过网址注意到了
http://localhost:9092/
我的浏览器下载了包含奇怪内容的文件:
如果要解码此文本,我会看到以下消息:
Version mismatch, driver version is “0” but server version is “15”
我使用h2版本1.4.182
最佳答案
H2 contains multiple servers :
- the TCP Server (对于 H2 JDBC 客户端),
- the Web Server (对于浏览器,H2 控制台应用程序),以及
- the PG Server (for PostgreSQL clients)。
您已启动 TCP 服务器。如果要使用浏览器,还需要启动Web Server:
private static void startH2(){
Server tcpServer = null;
Server webServer = null;
try {
tcpServer = Server.createTcpServer("-tcpAllowOthers").start();
System.out.println("TCP Server Port: " + tcpServer.getPort());
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:tcp://localhost/~/test22;MODE=PostgreSQL", "sa", "");
webServer = Server.createWebServer().start();
System.out.println("Web Server (H2Console) Port: " + webServer.getPort());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finish");
}
关于java - h2 不以编程方式启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318611/