1

My requirement is H2 database should get started while deploying my war file in embedded mode. For that I created class(DbSetup) in my java project. I mentioned the same class in web.xml . Then I implemented contextInitialized(ServletContextEvent sce) method. In that method I am making connection to h2 db to which I made connection while deploying war. While getting war file, I am getting exception like " No suitable class found for jdbc:h2:~/test1. Could you please how to approach this problem? I am binding h2-1.4.192.jar with my war. I also copied the same jar to class path.

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>

my class:

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
damar
  • 11
  • 1

0 Answers0