当前位置:  开发笔记 > 编程语言 > 正文

如何显示本地h2数据库(Web控制台)的内容?

如何解决《如何显示本地h2数据库(Web控制台)的内容?》经验,为你挑选了1个好方法。

最近我加入了一个新的团队,这里的人们使用h2进行存根服务.

我想知道我是否可以使用Web界面显示此数据库的内容.在工作中,它可以去localhost:5080

我有一个使用h2数据库的项目,但是当我点击时我看不到h2 web控制台 localhost:5080

我也试过localhost:8082- 它也行不通.

我的项目配置(成功运行):

     
        
     

     
            
        

        
            
            
            
            
        

        
            
            
                classpath:hibernate-test.cfg.xml
            
            
                
                    false
                    UTF-8
                    true
                    create-drop
                
            
        

        

我没有想法如何访问h2 web控制台.请帮忙.

PS

我只在.m2文件夹中看到提到h2

PS2

我注意到,http://localhost:8082/如果在配置中替换url,可以使用Web控制台:


但是如果我已经启动了h2(在.m2文件夹中查找h2jar文件并点击双击)

如果我在启动应用程序时未启动h2 - 我看到以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
    ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbInitializer': Invocation of init method failed; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    ...
Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    ...
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (?????????? ?????????: "java.net.ConnectException: Connection refused: connect: localhost"
Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-182])
    at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
    ...
Caused by: org.h2.jdbc.JdbcSQLException: ?????????? ?????????: "java.net.ConnectException: Connection refused: connect: localhost"
Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-182]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    ...
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    ...

我希望实现h2启动,如果它在我启动应用程序时没有启动.

PS3

我试过写下面的代码:

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

在我尝试输入localhost:9092浏览器后执行它.

此时下载文件.在inside文件中有以下内容:

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

我的h2版本1.4.182

PS4

此代码有效:

public class H2Starter extends ContextLoaderListener {
    private static final Logger LOG = LoggerFactory.getLogger(H2Starter.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {

        startH2();
        super.contextInitialized(event);
    }

    private static void startH2() {

        try {
            Server.createTcpServer("-tcpAllowOthers").start();
            Class.forName("org.h2.Driver");
            DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL;AUTO_SERVER=TRUE", "sa", "");

            Server.createWebServer().start();
        } catch (Exception e) {
            LOG.error("cannot start H2 [{}]", e);
        }
    }

    public static void main(String[] args) {
        startH2();
    }
}

但是我需要在混凝土弹簧轮廓激活时调用它(现在它始终有效)



1> WeMakeSoftwa..:

我们将问题分成两部分.

根据您指定H2连接的方式,您将获得不同的操作模式.

模式包括:嵌入式,内存中,服务器.

jdbc:h2:~/test在嵌入模式下为您提供H2实例.嵌入模式有一个限制,只能通过相同的类加载器和相同的JVM访问(证明)

jdbc:h2:mem:test获取内存中的H2实例.外部世界也无法获得这一点.

jdbc:h2:tcp://localhost/test将启动H2服务器,它可以从JVM 服务器模式外部访问,但有一个限制 - 服务器需要在建立连接之前启动.

最后一个限制是导致您的Connection refused: connect: localhost"异常.

总结一下:

启动应用程序之前启动H2服务器

使用jdbc:h2:tcp://localhost/test的连接字符串

....

快乐的编码:)

更新

刚刚注意到你想在启动应用程序的过程中启动服务器.

您可以通过多种方式执行此操作,具体取决于您如何启动应用程序:

如果你正在使用maven/gradle,你可以更容易地添加一些配置文件/任务,以便在应用程序实际启动之前执行它.

如果你必须在java中设置所有内容,我建议你看一下这个问题

更新2

如果仅为了开发/调试目的需要连接到本地数据库,我将使用maven配置文件设置所有内容.这个问题的答案将解决这个问题.

如果您需要在生产中访问H2数据库(我很难想象有任何用例),最好在春天这样做.主要是因为应用程序容器/环境设置可能在生产中有所不同(与开发环境相比).

要解决有关是否在Spring上下文之外启动服务器的问题 - 这一切都取决于要求.您应该注意的一件事是服务器应该在数据源启动之前启动(否则弹出上下文不会加载)

更新3

不幸的是,我无法为您提供有效的解决方案,但根据JavaDocs,TCP服务器和Web服务器之间存在差异.仔细研究H2 Server类的JavaDoc.

我想你应该使用Server.createWebServer()方法来创建服务器(TCP服务器和Web服务器之间的区别就是那个

你可以使用的另一个很棒的类org.h2.tools.Console(这里是JavaDoc)只需运行Console的主要方法,我想这应该可以解决所有问题.

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有