如何配置tomcat数据源以及Cannot create JDBC driver of class '' for connect URL 'null'
来源:互联网 发布:数据统计分析公司 编辑:程序博客网 时间:2024/06/15 23:59
最近折腾TOMCAT数据源的时候,遇到了一个错误:Cannot create JDBC driver of class '' for connect URL 'null'。
配置某个web项目私有的数据源,比如我的web项目名叫jndi:
1.将数据库驱动放到WEB-INF/lib下
2.在META-INF下建立context.xml
<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/mysql"auth="Container" type="javax.sql.DataSource"initialSize="5"maxActive="5" maxIdle="5"username="root" password="root"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/test"factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"/></Context>
3.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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
4.启动web项目可以通过下面的代码验证是否可以成功获取到数据源和数据库连接
<%@ page contentType="text/html;charset=utf-8"%><%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %><%Connection conn = null;try{Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");System.out.println(ds.getClass()); conn = ds.getConnection();System.out.println(conn.getClass()); System.out.println(conn.hashCode()); } catch (Exception e){e.printStackTrace();}finally{conn.close();}%>
1.将数据库驱动加入到%TOMCAT_HOME%/lib下
2.在%TOMCAT_HOME%/conf/server.xml中的<GlobalNamingResources>节点下增加配置:
<!--配置MySQL数据库的JNDI数据源--><Resource name="jdbc/mysql"auth="Container" type="javax.sql.DataSource"initialSize="5"maxActive="5" maxIdle="5"username="root" password="root"driverClassName="com.mysql.jdbc.Driver"factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"url="jdbc:mysql://localhost:3306/test"/>
3.既然已经配置了全局数据库连接,我们就可以删除jndi项目下的数据库驱动jar和context.xml,然后访问jsp发现:不能获取数据库连接。
折腾了很久,最终在这篇博客“在Tomcat配置JNDI数据源的三种方式”中找到了解决方案:还必需在jndi这个web项目下提供context.xml,内容配置如下:
<?xml version="1.0" encoding="UTF-8"?> <Context> <ResourceLink name="jdbc/mysql" type="javax.sql.DataSource" global="jdbc/mysql"/> </Context>
值得一提的是:如果context.xml配置如上所示,那么我们在web.xml中可以不用再配置如下数据源的引用:
<resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
0 0
- 如何配置tomcat数据源以及Cannot create JDBC driver of class '' for connect URL 'null'
- tomcat 5.5 Cannot create JDBC driver of class '' for connect URL 'null' 配置数据源错误解决办法
- Tomcat6配置数据源:Cannot create JDBC driver of class '' for connect URL 'null'解决方案
- tomcat7配置数据源异常---Cannot create JDBC driver of class '' for connect URL 'null'
- JNDI数据源局部配置(解决Cannot create JDBC driver of class '' for connect URL 'null')
- Tomcat配置数据源(异常:Cannot create JDBC driver of class '' for connect URL )
- IBatis配置tomcat的JNDI问题解决Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- cannot create jdbc driver of class '' for connect url 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- Cannot create JDBC driver of class '' for connect URL 'null'
- UITableViewCell滑动删除及移动
- RabbitMQ课程翻译(第一课)
- JSP页面展示多个PDF文件
- 对HTTP的简单理解
- 我的Android之路
- 如何配置tomcat数据源以及Cannot create JDBC driver of class '' for connect URL 'null'
- Android监听程序进入后台,或者恢复到前台
- MyEclipse设置JVM内存
- Centos_Hadoop2.7.1环境搭建记录
- 2016年03月01日
- ios 博客
- 堆区(heap)和栈区(stack)的区别
- javascript入门
- STM32启动文件分析