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