在看《精通JSP》-张新曼著时的连接池问题
环境:Tomcat 5.5.17中文版、
数据库驱动:mysql-connector-java-3.1.12-bin.jar
一、
按照书上的说明,在Tomcat的管理页面中配置了数据源,在server.xml文件生成内容如下: <Resource
name= "jdbc/mysql "
type= "javax.sql.DataSource "
password= "123456 "
driverClassName= "com.mysql.jdbc.Driver "
maxIdle= "40 "
maxWait= "5000 "
username= "root "
url= "jdbc:mysql://localhost:3306/netshop "
maxActive= "40 "/>
二、
web.xml配置内容如下:
<resource-ref>
<description> mysqlDB Connection </description>
<res-ref-name> jdbc/mysql </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
三、
context.xml文件内容如下:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<Context>
<ResourceLink name= "jdbc/mysql " global= "jdbc/mysql " type= "javax.sql.DataSource "/>
</Context>
四、JSP文件如下:
<%@ page contentType= "text/html;charset=gb2312 " %>
<%@ page import= "java.sql.* "%>
<%@ page import= "javax.sql.* "%>
<%@ page import= "javax.naming.* "%>
<%@ page session= "false " %>
<html>
<head>
<title> MySQL 数据源测试 </title>
<center>
<%
out.print( "MySQL 数据源测试开始... "+ " <br/> ");
DataSource ds = null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup( "java:comp/env/jdbc/mysql ");
Connection conn = ds.getConnection();
conn.close();
out.print( "MySQL 数据源测试成功! ");
}
catch(Exception ex){
out.print( "出现意外,信息是: "+ex.getMessage());
ex.printStackTrace();
}
%>
</center>
</head>
</html>
五、产生的错误如下:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ' ' for connect URL 'null '
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at org.apache.jsp.MySQLDSTest_jsp._jspService(MySQLDSTest_jsp.java:61)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:243)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
... 22 more
重新搞了又搞,网上查的资料都是tomcat4.x版本的,反正就是搞不定,谁知道的帮帮忙呀。
[解决办法]
: <Resource
name= "jdbc/mysql "
type= "javax.sql.DataSource "
password= "123456 "
driverClassName= "com.mysql.jdbc.Driver "
maxIdle= "40 "
maxWait= "5000 "
username= "root "
url= "jdbc:mysql://localhost:3306/netshop "
maxActive= "40 "/>
你这一段是不是放错了位置 你坊在了server。xml的什么地方??