SQLNestedException: Cannot create JDBC driver

ぃ、小莉子 提交于 2019-11-30 20:06:14

用DataSource 通过JNDI取得连接问题,抛出如下异常:

java.lang.RuntimeException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

原因是:部署webapp应用方式的问题,导致tomcat启动时找不到/META-INF/context.xml文件

往tomcat部署webapp应用有很多方式:

第1种方式:这种方式最简单,就是在$CATALINA_BASE/conf/server.xml文件里做如下配置:

<Context docBase="D:\java\workspace\JspServletNote\WebContent" path="/JspServletNote" reloadable="true" />

这种方式,就能导致tomcat启动时找不到/META-INF/context.xml文件

解决方法就是:把自个的WebAppProject里的/META-INF/context.xml的内容复制到$CATALINA_BASE/conf/context.xml里或者$CATALINA_BASE/conf/server.xml里 参见:http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

第2种方式:直接将自个的WebAppProject打包成WAR文件拷贝到$CATALINA_BASE/webapps目录里,tomcat启动后,自动会将WebAppProject里的/META-INF/context.xml文件内容复制到$CATALINA_BASE/conf/context.xml里,打开这个context.xml文件会多了如下内容:

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/demo" password="123456" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/demo" username="artshell"/>

第3种方式:右击WebAppProject —> Run on Server  —>finish,这种方式和第2种方式其实是一样的部署方式,只不过是创建了一个tomcat实例来进行部署,打开/workspace/Servers/Tomcat v7.0 Server at localhost-config/context.xml文件,里边也有<Resource ....... />内容

这里转载另一篇文章,也是关于此类问题: http://blog.csdn.net/jljf_hh/article/details/1958194

nnection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

<Resource name="jdbc/slash" auth="Container" type="javax.sql.DataSource"/>   
<ResourceParams name="jdbc/slash">   
  <parameter>   
            <name>factory</name>   
            <value>org.apache.commons.dbcp.BasicDataSourceFactory<value>   
        </parameter>     
  <parameter>   
  <name>driverClassName</name>   
  <value>com.mysql.jdbc.Driver<value>   
  </parameter>      
  <parameter>   
  <name>url</name>   
  <value>jdbc:mysql://localhost/fzzslash<value>   
  </parameter>   
</ResourceParams>
方案1:把上面的内容缩成一行,如下:
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"   
          maxActive="100" maxIdle="30" maxWait="10000"   
          username="javauser" password="javadude" 
          driverClassName="com.mysql.jdbc.Driver"  
          url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
方案2:

解决:在%TOMCAT_HOME%/conf/Catalina/localhost下找到你的web应用对应的.xml文件,如test.xml,并在此文件的下添入代码:

<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>

重启tomcat。

你的是服务器的全局JNDI资源,而用InitialContext去找server的resource当然找不到了,要想找到server的resource就得在web application中的context环境里加入一个指向该全局resource的ResourceLink。

global -->The name of the linked global resource in the global JNDI context.
name -->The name of the resource link to be created, relative to the java:comp/env context.?
type -->The fully qualified Java class name expected by the web application when it performs a lookup for this resource link.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!