Cannot create JDBC driver of class '' for connect URL 'null'问题解决
Cannot create JDBC driver of class '' for connect URL 'null'问题解决方法1
Cannot create JDBC driver of class '' for connect URL 'null'问题解决方法2
1)启动Tomcat服务器,打开浏览器,输入http://localhost:8080/admin(其中localhost是名称服务器或称为主机),
进入管理界面的登陆页面,这时候请输入原来安装时要求输入的用户名和密码,登陆到管理界面,
2)选择Resources-Data sources进入配置数据源界面,选择
Data Source Actions ->选择Create New
Data Source,进入配置详细信息界面
主要内容例如下:
JNDI Name: ->jdbc/mysql
Data Source URL
->jdbc:mysql://localhost:3306/test
JDBC Driver Class-> org.gjt.mm.mysql.Driver
3)修改\conf\Catalina\localhost目录下建立一个xml文件,名称为你所发布的web应用的名称.xml,(如testpool.xml)打开添加内容如下:
<?xml version="1.0"
encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3306/test"
maxActive="4"/>
</Context>
内容同conf/server.xml中<GlobalNamingResources>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="123456"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="50"
username="root"
url="jdbc:mysql://localhost:3306/test"
maxActive="4"/>
</GlobalNamingResources>
少了这一步会报错:Cannot create JDBC driver of class '' for connect URL
'null'
4)修改web.xml
打开%TOMCAT_HOME%\conf\web.xml或yourwebapp/web-inf/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>
注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。
到这里,配置工作就基本完成了!
5)引用JNDI时用"java:comp/env/jdbc/mysql";
建立文件测试 test.jsp:
<%@page contentType="text/html;charset=utf-8"
%>
<%@page import="java.sql.*"
%>
<%@page import="javax.sql.*"
%>
<%@page import="javax.naming.*"
%>
<html>
<head>
<title>Tomcat连接池测试</title>
</head>
<body>
<%
Context ctx=new InitialContext();
Connection conn=null;
DataSource
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
Statement
stmt=conn.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from
testexample");
while(rs.next()){
out.println(rs.getInt(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
}
out.println("数据库操作成功!");
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>