我的要求是H2数据库应该在嵌入模式下部署war文件时开始。为此,我在我的java项目中创建了类(DbSetup)。我在web.xml中提到了同一个类。然后我实现了contextInitialized(ServletContextEvent sce)方法。在那个方法中,我正在连接到我在部署war时与之建立连接的h2 db。在获取战争档案的同时,我得到的例外是"没有为jdbc找到合适的类:h2:〜/ test1。你能问一下如何解决这个问题吗?我和我的战争约束h2-1.4.192.jar。我还将同一个jar复制到类路径。
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CEMDBWS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>
<context-param>
<param-name>db.url</param-name>
<param-value>"jdbc:h2:~/test1"</param-value>
</context-param>
<context-param>
<param-name>db.user</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>db.password</param-name>
<param-value>sa</param-value>
</context-param>
<listener>
<listener-class>DbSetup</listener-class>
</listener>
<servlet>
<servlet-name>JSON TO SQL Rest API</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSON TO SQL Rest API</servlet-name>
<url-pattern>/Request/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>-1</session-timeout>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<mime-mapping>
<extension>json</extension>
<mime-type>application/json</mime-type>
</mime-mapping>
我的班级:
try
{
Connection connection = null;
final String DB_CONNECTION = "jdbc:h2:~/test1";
final String DB_USER = "sa";
Class.forName(org.h2.Driver);
final String DB_PASSWORD = "sa";
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
return connection;
} catch (SQLException e) {
LOGGER.error("Error while getting DB connection", e);
return connection;
} catch (ClassNotFoundException e) {
LOGGER.error("Error while loading Driver", e.getMessage());
}
return connection