SQLNestedException: Cannot create JDBC driver

用DataSource 經過JNDI取得鏈接問題,拋出以下異常: html

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文件 java

往tomcat部署webapp應用有不少方式: mysql

第1種方式:這種方式最簡單,就是在$CATALINA_BASE/conf/server.xml文件裏作以下配置: web

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

這種方式,就能致使tomcat啓動時找不到/META-INF/context.xml文件 sql

解決方法就是:把自個的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
shell

第2種方式:直接將自個的WebAppProject打包成WAR文件拷貝到$CATALINA_BASE/webapps目錄裏,tomcat啓動後,自動會將WebAppProject裏的/META-INF/context.xml文件內容複製到$CATALINA_BASE/conf/context.xml裏,打開這個context.xml文件會多了以下內容: apache

<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 ....... />內容 tomcat

這裏轉載另外一篇文章,也是關於此類問題: 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'
app

<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.

相關文章
相關標籤/搜索