简体   繁体   English

Tomcat 9 / JNDI 数据源 - 无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序

[英]Tomcat 9 / JNDI DataSource - Cannot create JDBC driver of class '' for connect URL 'null'

  • Java Version: 10.0.1 Java 版本:10.0.1
  • Tomcat Version: 9.0.10 Tomcat 版本:9.0.10
  • Postgres Driver Version: 42.2.4 Postgres 驱动程序版本:42.2.4

I have seen one similar question on stackoverflow, but it is a bit different from mine and none of the solutions there seemed to work for me: Cannot create JDBC driver of class ' ' for connect URL 'null' : I do not understand this exception我在 stackoverflow 上看到过一个类似的问题,但它与我的有点不同,那里的解决方案似乎都不适合我: 无法为连接 URL 'null' 创建类 ' ' 的 JDBC 驱动程序:我不明白这个异常

This is my first time using Tomcat 9 and the first time I've tried using JNDI for DataSources.这是我第一次使用 Tomcat 9,也是我第一次尝试将 JNDI 用于数据源。

Using doc as reference: https://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html使用 doc 作为参考: https : //tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html

It seems like it isn't able to read the driver/url/etc properties from the <Resource> ... though I cannot for the life of me figure out why.它似乎无法从 <Resource> 读取驱动程序/url/etc 属性......尽管我终生无法弄清楚原因。

Error:错误:

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:432)
org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Setup/Config:设置/配置:

postgresql-42.2.4.jar in tomcat lib. tomcat 库中的 postgresql-42.2.4.jar。

server.xml snippet: server.xml 片段:

<GlobalNamingResources>
. . .
<Resource name="jdbc/xxxx" auth="Container"
        type="javax.sql.DataSource"
        driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://127.0.0.1:5432/xxxx"
        username="xxxx"
        password="xxxx"
        maxTotal="20"
        maxIdle="10"
        maxWaitMillis="15000" />
</GlobalNamingResources>

Web App web.xml:网络应用程序 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <resource-ref>
        <description>xxxx Connection</description>
        <res-ref-name>jdbc/xxxx</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <servlet>
        <servlet-name>Anwinity Test App</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.anwinity.webapps.test</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Anwinity Test App</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

Java Resource Class: Java资源类:

package com.anwinity.webapps.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("test")
public class TestResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response test() throws Exception {

        // InitialContext ctx = new InitialContext();
        // DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xxxx");
        Context initContext = new InitialContext();
        Context envContext = (Context)initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource)envContext.lookup("jdbc/xxxx");
        if(ds == null) {
            System.out.println(":(");
            return Response.noContent().build();
        }
        else {
            try(Connection conn = ds.getConnection()) {
                String sql = "select * from test";
                try(Statement stmt = conn.createStatement()) {
                    List<Integer> list = new ArrayList<>();
                    try(ResultSet rs = stmt.executeQuery(sql)) {
                        while(rs.next()) {
                            int i = rs.getInt(1);
                            System.out.println(i);
                            list.add(i);
                        }
                        TestResponse resp = new TestResponse();
                        resp.setN(list.stream().mapToInt(i -> i).toArray());
                        return Response.ok(resp).build();
                    }
                }
            }
        }

    }
}

您正在 web.xml 文件中定义资源引用,请尝试使用 context.xml 文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在tomcat 6中创建和使用数据源时,无法为连接URL&#39;null&#39;-:ERROR创建类&#39;&#39;的JDBC驱动程序 - Cannot create JDBC driver of class '' for connect URL 'null' -:ERROR while creating and using a Datasource in tomcat 6 带有mysql的Tomcat:“无法为连接URL&#39;null&#39;创建类”的JDBC驱动程序” - Tomcat with mysql : “ Cannot create JDBC driver of class '' for connect URL 'null' ” 无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序:Tomcat和SQL Server JDBC驱动程序 - Cannot create JDBC driver of class '' for connect URL 'null' : Tomcat & SQL Server JDBC driver Godaddy JNDI问题-无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序 - Godaddy JNDI Problem---Cannot create JDBC driver of class '' for connect URL 'null' 无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序:Tomcat + MySQL + Spring MVC - Cannot create JDBC driver of class '' for connect URL 'null' : Tomcat + MySQL + Spring MVC Tomcat,Java和SQL Server 2008 R2:无法为连接URL&#39;&#39;创建类&#39;&#39;的JDBC驱动程序 - Tomcat, Java & SQL Server 2008 R2: Cannot create JDBC driver of class '' for connect URL 'null' Tomcat 8 + MySQL + Spring + JPA-无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序 - Tomcat 8 + MySQL + Spring + JPA - Cannot create JDBC driver of class '' for connect URL 'null' Java Spring Tomcat8:无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序 - Java Spring Tomcat8: Cannot create JDBC driver of class '' for connect URL 'null' 在spring tomcat应用程序中无法为连接URL&#39;null创建类&#39;&#39;的JDBC驱动程序 - Getting Cannot create JDBC driver of class '' for connect URL 'null in spring tomcat application 无法一直为连接URL&#39;null创建类&#39;&#39;的JDBC驱动程序 - Cannot create JDBC driver of class '' for connect URL 'null all the time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM