无法为连接网址"null"创建类“"的JDBC驱动程序; -HikariCP,Tomcat8,PostgreSQL [英] Cannot create JDBC driver of class '' for connect URL 'null" - HikariCP, Tomcat8, PostgreSQL

查看:179
本文介绍了无法为连接网址"null"创建类“"的JDBC驱动程序; -HikariCP,Tomcat8,PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我之前从未设置过JDBC Pool(因此,如果这看起来很平凡,那么我感到抱歉).我一直在尝试解决问题,但无济于事.我已经探索了其他各种stackoverflow帖子中的建议选项,但都没有成功.

Firstly I have never setup a JDBC Pool before (So i am sorry if this seems mundane). I have been trying for some time to solve the problem but to no avail. I have explored the suggested options from various other stackoverflow posts but none have been successful.

我尝试过:

我将HikariCP与PostgreSQL(pgjdbc-ng)驱动程序一起使用. Tomcat8部署了我使用maven构建的war文件. web.xml,context.xml,Java代码.

I am using HikariCP with a PostgreSQL(pgjdbc-ng) driver. Tomcat8 deploys my war file which i build using maven. web.xml, context.xml, java code.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name>Restful Web Application</display-name>
  <resource-ref>
    <res-ref-name>jdbc/postgresHikari</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
  <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.seng402.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

context.xml

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/postgresHikari" auth="Container"
    factory="com.zaxxer.hikari.HikariJNDIFactory"
    type="javax.sql.DataSource"
    minimumIdle="5" 
    maximumPoolSize="10"
    connectionTimeout="300000"
    dataSource.implicitCachingEnabled="true" 
    dataSource.user="docker"
    dataSource.password="docker"
    jdbcUrl="jdbc:postgresql://192.168.59.103:5432/docker"
    driverClassName="com.impossibl.postgres.jdbc.PGDataSource"/>
</Context>

java代码

Context initCtx = null;
    try {
        initCtx = new InitialContext();
        Context envCtx;
        try {
            envCtx = (Context) initCtx.lookup("java:comp/env");
            // Look up our data source
            DataSource ds = (DataSource) envCtx.lookup("jdbc/postgresHikari");
            try {
                // Allocate and use a connection from the pool
                Connection conn = ds.getConnection(); // throws the error
                conn.close();
                System.out.println("Connected!");
            } catch (SQLException e) {
                System.out.println("Failed to Connect!");
                e.printStackTrace();
            }
        } catch (NamingException e) {
            System.out.println("Failed to Lookup!");
            e.printStackTrace();
        }
    } catch (NamingException e1) {
        System.out.println("Fail to get Context!");
        e1.printStackTrace();
    }

输出: 连接失败!

java.sql.SQLException:无法为连接URL'null'创建类"的JDBC驱动程序

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'

ds.getConnection()引发错误. 关于如何解决此问题或进一步调试的任何建议将不胜感激.

ds.getConnection() throws the error. Any suggestions as to how i could fix this or debug further would be greatly appreciated.

已解决!!! -context.xml未被放入META-INF文件夹中

推荐答案

context.xml未被放入META-INF文件夹

context.xml was not being put into the META-INF folder

这篇关于无法为连接网址"null"创建类“"的JDBC驱动程序; -HikariCP,Tomcat8,PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆