如何显示本地h2数据库(Web控制台)的内容?
最近我加入了一个新团队,这里的家伙使用h2进行存根服务。
我想知道我是否可以使用Web界面显示此数据库的内容。在工作中,可以通过转到localhost:5080
我有一个使用h2数据库的项目,但是当我点击时,我看不到h2 Web控制台localhost:5080
我也试过 - 它也不起作用。localhost:8082
我的项目配置(成功工作):
<bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">
<property name="targetName" value="dataSource" />
</bean>
<bean id="wrappedDataSource" class="net.bull.javamelody.SpringDataSourceFactoryBean">
<property name="targetName" value="dataSource" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/test;MODE=PostgreSQL" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="wrappedDataSource"/>
<property name="configLocation">
<value>classpath:hibernate-test.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>
<context:property-placeholder location="classpath:jdbc.properties"/>
我不知道如何访问h2 Web控制台。请帮忙。
附言
我看到仅在文件夹中提到h2.m2
附言 2
我注意到 Web 控制台可用,如果将配置中的 url 替换为:http://localhost:8082/
<property name="url" value="jdbc:h2:tcp://localhost/~/test;MODE=PostgreSQL" />
但是如果我已经启动了h2(在文件夹中找到jar文件并双击),它就可以了).m2
h2
如果我启动应用程序时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 在我启动应用程序时未启动时启动。
附言 3
我试图写下面的代码:
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
此时下载文件。内部文件以下内容:
Version mismatch, driver version is “0” but server version is “15”
我的 h2 版本 1.4.182
附言 4
此代码的工作原理:
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();
}
}
但我需要仅在混凝土弹簧轮廓激活时才调用它(现在它始终工作)