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>
