java - h2 不以编程方式启动

标签 java h2 h2db

我写了以下代码:

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

我运行我的主要方法并看到以下情况:

enter image description here

看起来Server.createTcpServer创建了新的非守护线程。

但是通过 url localhost:8082 我看不到 h2 Web 控制台(实际结果 - ERR_CONNECTION_REFUSED)

如何解决这个问题?

附注

我通过网址注意到了

http://localhost:9092/

我的浏览器下载了包含奇怪内容的文件:

enter image description here

如果要解码此文本,我会看到以下消息:

Version mismatch, driver version is “0” but server version is “15”

我使用h2版本1.4.182

最佳答案

H2 contains multiple servers :

您已启动 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/

相关文章:

java - 我有一个属性类,我在其中设置并获取英雄属性和英雄类,我在其中设置英雄信息,我希望游戏在我的主程序中处理

java - 如何在 Spring 框架中安排任务每 'N' 秒发生一次

spring-boot - 如何解决h2数据库和spring boot中错误的用户名和密码错误?

java - RxJava2 flatMap 创建重复事件

java - 在 Java 中,将变量直接分配给包装类是一个好的做法吗?

java - 使用 hibernate.hbm2ddl.import_files 执行 sql 脚本时出错?

h2 - 检测和恢复故障的H2群集节点

java - 访问 H2 数据库架构

java - H2用户自定义函数被多次调用

java - 如何在 Spring Boot 应用程序中使用 Flyway 启动 H2 db TCP 服务器