在配置数据源后,用JSP来测试,总出现 “出现意外,信息:Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/ml',希望大家帮个忙,来解决下,我把代码都贴上来了。我换了好几个数据库的连接驱动都一样的 。
在server.xml文件中<GlobalNamingResources>
<Resource  name="jdbc/mysql" type="javax.sql.DataSource" password="123456"
              driverClssName="org.gjt.mm.mysql.Driver" maxIdle="100" maxWait="5000" username="root" url="jdbc:mysql://localhost:3306/ml" maxActive="40"/>
</GlobalNamingResources>在context .xml中加入
<Context>
<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSource"/>
</Context>在工程项目的\WebRoot\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>其其测试页面为MySQLDSTest.jsp
<%@ page language="java" pageEncoding="gbk"%><%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*" %>
<%@ page session="false" %>
<html:html lang="true">
  <head>
    <html:base />
    
    <title>MySQLDSTest.jsp</title>  </head>
  
  <body>
   <%
       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.println("MySQL数据源测试成功!!!");
       }catch(Exception ex){
           out.print("出现意外,信息:"+ex.getMessage());
           ex.printStackTrace();
       }
       
   %>
  </body>
</html:html>
测试之后就出现以下问题:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/mldn'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.apache.jsp.MySQLDSTest_jsp._jspService(MySQLDSTest_jsp.java:102)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.lxh.filter.EncodingFilter.doFilter(EncodingFilter.java:25)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:264)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
... 23 more

解决方案 »

  1.   

    LZ你url="jdbc:mysql://localhost:3306/ml" 是这个,
    但是异常报的URL 'jdbc:mysql://localhost:3306/mldn' 这个,你的到底是哪个,还有,你有没有把驱动程序包加进去
      

  2.   

    1、lz 可以直接把这段文件放在context.xml中
    <Resource  name="jdbc/mysql" type="javax.sql.DataSource" password="123456" 
                  driverClssName="org.gjt.mm.mysql.Driver" maxIdle="100" maxWait="5000" username="root" url="jdbc:mysql://localhost:3306/ml" maxActive="40"/> 
    后面的配置也就不需要
    2、不行的话,查一下驱动的版本
    3、换个Tomcat,有时服务器版本也有问题
      

  3.   

    MySQL5.0+Tomcat6.0的数据源配置:在Java Web开发中都要与数据库打交道,为了不频繁地打开和关闭数据库,以减少数据库操作负荷,可使数据库在开发过程中保持打开状态,在这里我们采用配置数据源的方式(JNDI),而不是传统地JDBC方式。下面就针对常规型的MySQL5.0.15和Tomcat6.0的数据源的基本配置进行简单的介绍: 
      首先声明,如果数据源没有配置好的话,在开发过程中会抛出诸如下列异常等: 
      1、org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 
      2、Caused by: java.sql.SQLException: No suitable driver 
      3、Name jdbc is not bound in this context 
    现在开始讲下如何配置好数据源同时也是解决上述异常的办法: 
      方案一: 
      步骤一、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context> 中加入如下代码(要根据自己的情况稍加修改): <Resource name="jdbc/pss"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  (也可以为com.mysql.jdbc.Driver)url="jdbc:mysql://localhost:3306/spdb1"  maxActive="100"  maxIdle="30"  maxWait="5000"/> 
      步骤二、在工程应用中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改): 
    <resource-ref> 
        <description>MySQL DataSource</description> 
         <res-ref-name>jdbc/pss</res-ref-name> 
         <res-type>javax.sql.DataSource</res-type> 
         <res-auth>Container</res-auth> 
    </resource-ref> 
    步骤三、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。 
        通过这三步,一个基本的数据源就配置成功了! 
      方案二 
    步骤一、在Tomcat6.0解压目录conf下找到server.xml,在其中的<GlobalNamingResources></GlobalNamingResources>中加入如下代码(要根据自己的情况稍加修改): 
    <Resource name="jdbc/pss"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/pss"  maxActive="100"  maxIdle="30"  maxWait="5000"/> 
    步骤二、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context>中加入并修改成如下代码(要根据自己的情况稍加修改): 
    <Context path="/znt" debug="1" reloadable="true"  docBase="E:\EclipseWorkPlace\pss\WebRoot"> 
    <ResourceLink global="jdbc/pss" name="jdbc/pss" type="javax.sql.Datasource"/> 
    ................<!--此间可能有系统其它自配的内容,可不管--> 
    </Context> 
    步骤三、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改): 
    <resource-ref> 
        <description>MySQL DataSource</description> 
         <res-ref-name>jdbc/pss</res-ref-name> 
         <res-type>javax.sql.DataSource</res-type> 
         <res-auth>Container</res-auth> 
    </resource-ref> 
    步骤四、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。 
       通过以上四步就好了! 
                                              方案三(具有不稳定性,慎用) 
    步骤一、在Tomcat6.0解压目录conf下找到server.xml,在其中的<Host></Host>中加入如下代码(要根据自己的情况稍加修改): 
    <Context path="/znt" docBase="E:\EclipseWorkPlace\pss\WebRoot" 
      debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" 
         prefix="localhost_MysqlTest_log." suffix=".txt" 
         timestamp="true"/> 
    <Resource name="jdbc/pss"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/pss"  maxActive="100"  maxIdle="30"  maxWait="5000"/> 
    </Context> 
    步骤二、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改): 
    <resource-ref> 
        <description>MySQL DataSource</description> 
         <res-ref-name>jdbc/pss</res-ref-name> 
         <res-type>javax.sql.DataSource</res-type> 
         <res-auth>Container</res-auth> 
    </resource-ref> 
    步骤三、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。 
        通过以上三步,大部分时候还是起作用的,但有时会出现异常,因此不建议使用。