嵌套异常是 java.sql.SQLException:无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序 - Spring MVC JNDI 问题

2024-02-16

我正在开发 SpringMvcJndiDataSourceXML 示例。在此示例中,我面临以下错误。我找到了很多链接,但没有一个对我有用。虽然这是重复的问题,但我仍然想得到以下问题的答案。请指导。

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:394)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:474)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:484)
    at net.codejava.spring.dao.UserDAOImpl.list(UserDAOImpl.java:24)
    at net.codejava.spring.controller.HomeController.home(HomeController.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    ... 43 common frames omitted
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:315)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2144)
    ... 47 common frames omitted

我使用tomcat进行部署。spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="net.codejava.spring" />

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/UsersDB"/>
    </bean>

    <bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
        <constructor-arg>
            <ref bean="dataSource" />
        </constructor-arg>
    </bean>
</beans>    

web.xml

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

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <!-- database JNDI reference -->
    <description></description>
    <resource-ref>
        <description>DBConnection</description>
        <res-ref-name>jdbc/test</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

tomcat 服务器.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
    license agreements. See the NOTICE file distributed with this work for additional 
    information regarding copyright ownership. The ASF licenses this file to 
    You under the Apache License, Version 2.0 (the "License"); you may not use 
    this file except in compliance with the License. You may obtain a copy of 
    the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
    by applicable law or agreed to in writing, software distributed under the 
    License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
    OF ANY KIND, either express or implied. See the License for the specific 
    language governing permissions and limitations under the License. --><!-- Note: A "Server" is not itself a "Container", so you may not define 
    subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html -->
<Server port="8005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
    <!-- Security listener. Documentation at /docs/config/listeners.html <Listener 
        className="org.apache.catalina.security.SecurityListener" /> -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener SSLEngine="on"
        className="org.apache.catalina.core.AprLifecycleListener" />
    <!-- Prevent memory leaks due to use of particular java/javax APIs -->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


    <Resource auth="Container" 
        driverClassName="com.mysql.jdbc.Driver"
        maxActive="20" maxIdle="10" 
        maxWait="-1" 
        name="jdbc/UsersDB" 
        type="javax.sql.DataSource"
        username="root" password="root"
        url="jdbc:mysql://localhost:3306/test" />

  <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html -->
    <GlobalNamingResources>
        <!-- Editable user database that can also be used by UserDatabaseRealm 
            to authenticate users -->
        <Resource auth="Container" description="User database that can be updated and saved"
            factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase"
            pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase" />
    </GlobalNamingResources>

    <!-- A "Service" is a collection of one or more "Connectors" that share 
        a single "Container" Note: A "Service" is not itself a "Container", so you 
        may not define subcomponents such as "Valves" at this level. Documentation 
        at /docs/config/service.html -->
    <Service name="Catalina">

        <!--The connectors can use a shared executor, you can define one or more 
            named thread pools -->
        <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" 
            minSpareThreads="4"/> -->


        <!-- A "Connector" represents an endpoint by which requests are received 
            and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html 
            (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) 
            Connector: /docs/apr.html Define a non-SSL/TLS HTTP/1.1 Connector on port 
            8080 -->
        <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
            redirectPort="8443" />
        <!-- A "Connector" using the shared thread pool -->
        <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" 
            connectionTimeout="20000" redirectPort="8443" /> -->
        <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 This connector uses 
            the NIO implementation that requires the JSSE style configuration. When using 
            the APR/native implementation, the OpenSSL style configuration is required 
            as described in the APR/native documentation -->
        <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" 
            maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" 
            sslProtocol="TLS" /> -->

        <!-- Define an AJP 1.3 Connector on port 8009 -->
        <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


        <!-- An Engine represents the entry point (within Catalina) that processes 
            every request. The Engine implementation for Tomcat stand alone analyzes 
            the HTTP headers included with the request, and passes them on to the appropriate 
            Host (virtual host). Documentation at /docs/config/engine.html -->

        <!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine 
            name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> -->
        <Engine defaultHost="localhost" name="Catalina">

            <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html 
                (simple how to) /docs/config/cluster.html (reference documentation) -->
            <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> -->

            <!-- Use the LockOutRealm to prevent attempts to guess user passwords 
                via a brute-force attack -->
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <!-- This Realm uses the UserDatabase configured in the global JNDI resources 
                    under the key "UserDatabase". Any edits that are performed against this UserDatabase 
                    are immediately available for use by the Realm. -->
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                    resourceName="UserDatabase" />
            </Realm>

            <Host appBase="webapps" autoDeploy="true" name="localhost"
                unpackWARs="true">

                <!-- SingleSignOn valve, share authentication between web applications 
                    Documentation at: /docs/config/valve.html -->
                <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" 
                    /> -->

                <!-- Access log processes all example. Documentation at: /docs/config/valve.html 
                    Note: The pattern used is equivalent to using pattern="common" -->
                <Valve className="org.apache.catalina.valves.AccessLogValve"
                    directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log"
                    suffix=".txt" />

                <Context docBase="SpringMvcJndiDataSourceXML" path="/SpringMvcJndiDataSourceXML"
                    reloadable="true" source="org.eclipse.jst.jee.server:SpringMvcJndiDataSourceXML" />
            </Host>
        </Engine>
    </Service>
</Server>

In 服务器.xml将资源置于<GlobalNamingResources>

<GlobalNamingResources>
     <Resource auth="Container" 
        driverClassName="com.mysql.jdbc.Driver"
        maxActive="20" maxIdle="10" 
        maxWait="-1" 
        name="jdbc/UsersDB" 
        type="javax.sql.DataSource"
        username="root" password="root"
        url="jdbc:mysql://localhost:3306/test" />
</GlobalNamingResources>

In 上下文.xml

<ResourceLink global="jdbc/UsersDB" name="jdbc/UsersDB" auth="Container" type="javax.sql.DataSource"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌套异常是 java.sql.SQLException:无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序 - Spring MVC JNDI 问题 的相关文章

随机推荐

  • 将 Oracle 数据库从一台 PC 转移到另一台 PC 的最简单方法是什么?

    我想把我的oracle数据库转移到另一台电脑上 最简单的方法是什么 有没有 GUI 工具可以做到这一点 APC 的答案是逻辑传输的方法 如果您询问是因为您只想一次性传输到另一台电脑 那么 安装相同版本的Oracle 第二台电脑 创建相同的目
  • 当更改视图的模型时,是替换模型更好还是创建新视图更好?

    我的 Backbone 应用程序中有一个电子邮件视图 它目前在中实例化view我的控制器的动作 它有点像这样 routes email id email email function id var email new Email id id
  • Android 嵌入式网络服务器

    我想在我的 Android 应用程序中嵌入一个网络服务器 连接到它的设备将具有两种功能 一个是发送一个 xml 文件 我可以解析该文件并执行一些操作 另一个是能够从我的 SD 卡下载文件 我找到了 nanoHTTPD 和 AndroidHT
  • Linux 上的 mysql2 gem 安装

    我正在尝试在 Linux 上安装 gem 但它给了我一个错误 An error occured while installing mysql2 0 3 11 and Bundler cannot continue Make sure tha
  • Rails 中是否可以有复合外键?

    假设以下数据模式 Usage client id resource type amount Billing client id usage resource usage type rate 在此示例中 假设我有多个资源 每个资源都可以通过多
  • Python json.loads 改变对象的顺序

    我有一个包含 JSON 对象的文件 它是通过以下方式加载的 with open data json r as input file input data input file read 此时 input data 仅包含一个字符串 现在我继
  • Grails 3:外部 Logback.groovy 文件

    我正在尝试将我的logback groovy通过在 my 中设置路径来设置文件build gradlebootrun 闭包中的文件 bootRun if Environment currentEnvironment Environment
  • 浮动创建重叠的 Div

    我有两个 div 一个在另一个里面 当我将里面的一个浮动到 左 时遇到了一些问题 问题是外部 div 不会扩展其高度以适应内部 div 内部的文本 由于这可能非常令人困惑 因此我将尝试用一些代码来解释它 HTML div Inner Div
  • gprof 命令未创建正确的 out.txt

    首先 我运行的是 MacOSX 10 7 1 我已经正确安装了 Xcode 4 和所有库 以便使用 C 语言 我在 shell 中运行 gprof 命令时遇到问题 我将逐步解释我正在做的事情以及我收到的输出 Step 1 roger cd
  • 如何为 if 和 while 语句编写简单的解析器? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我需要编写一个简单的解析器 将标记转
  • 无法获取 BatchedBridge,请确保您的捆绑包正确打包

    我在 Android 中使用 React Native 时遇到问题 无法获取 BatchedBridge 请确保您的捆绑包正确打包 在此处输入图像描述 1 1 我尝试了一切 将 npm react native react 和 packag
  • Oracle Entity Framework 提供程序不存储包含毫秒的 DateTime.Now

    我基本上和这个人有完全相同的问题 为什么我无法使用实体框架保存当前的 DateTime Now https stackoverflow com questions 18557546 why i cant save the current d
  • R 中的点计数

    假设我从 40 个 x y 对创建以下图片 现在给出黑色的附加点 我希望能够计算出有多少点落在蓝色区域内 有多少点落在蓝色区域之外 以一种非常有效的方式 因为我将在一个昂贵的循环中多次执行此操作 为了说明我正在寻找的内容 请考虑下图 所以正
  • SignalR:如何停止在页面重新加载时创建新连接

    您好 我正在开发一个聊天应用程序以及应用程序中的一些其他页面 一旦我登录 我就会维护用户的会话 我的主要目的是 每当其他用户连接到服务器时 用户应该收到通知 我面临的问题是每当我导航到应用程序中的其他页面时 连接就会丢失 如何停止此行为并继
  • 子类中抛出异常的标准是什么

    到目前为止我所知道的是 子类如果重写超类方法应该抛出相同的异常或异常的子类 例如 这是对的 class SuperClass public int doIt String str Integer data throws ArrayIndex
  • 在 Rails 模型中编写大型 SQL 的更好方法?

    使用了很多之后Arel that Rails提供糖代码 我遇到问题时 处理大型且复杂的 SQL 查询 但我无法很好地处理Arel方法 我喜欢Arel对于小事情 但是当事情变得混乱时 我更喜欢将代码分开 那么 对于我应该如何治疗我的大 有什么
  • ES2015/2016 'typeof varName === 'undefined` 的方式?

    我现在正沉迷于 ES2015 的豪华体验中 有几个项目 我想知道我是否可以摆脱令人讨厌的拐杖来检查undefined在新仙境里 有没有更短但仍然准确的方法typeof varName undefined ES2015 已经有了 我当然可以使
  • 当您在浏览器中输入 URL 时会发生什么[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 有人可以告诉我 从我在浏览器中输入 URL 到我在浏览器上看到该页面 幕后发生了什么 详细说明该过程将会有很大帮助 注意 这是一个非常粗糙和过于简单化
  • Boost Log 更改默认logging::core 格式化程序?

    我正在使用 Boost v1 54 我想简单地更改logging core的默认接收器格式 但还没有找到方法 在里面日志记录格式 http www boost org doc libs 1 54 0 libs log doc html lo
  • 嵌套异常是 java.sql.SQLException:无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序 - Spring MVC JNDI 问题

    我正在开发 SpringMvcJndiDataSourceXML 示例 在此示例中 我面临以下错误 我找到了很多链接 但没有一个对我有用 虽然这是重复的问题 但我仍然想得到以下问题的答案 请指导 org springframework jd