代码之家  ›  专栏  ›  技术社区  ›  Vicky

获取java.lang.ClassNotFoundException:com.mysql.jdbc.driver异常

  •  1
  • Vicky  · 技术社区  · 14 年前

    在Tomcat中配置连接池时出现以下异常

    这是context.xml

    <Context path="/DBTest" docBase="DBTest"
            debug="5" reloadable="true" crossContext="true">
    
        <!-- maxActive: Maximum number of dB connections in pool. Make sure you
             configure your mysqld max_connections large enough to handle
             all of your db connections. Set to -1 for no limit.
             -->
    
        <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
             Set to -1 for no limit.  See also the DBCP documentation on this
             and the minEvictableIdleTimeMillis configuration parameter.
             -->
    
        <!-- maxWait: Maximum time to wait for a dB connection to become available
             in ms, in this example 10 seconds. An Exception is thrown if
             this timeout is exceeded.  Set to -1 to wait indefinitely.
             -->
    
        <!-- username and password: MySQL dB username and password for dB connections  -->
    
        <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
             org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
             Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
             -->
    
        <!-- url: The JDBC connection url for connecting to your MySQL dB.
             -->
    
      <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql:///BUSINESS"/>
    
    </Context>
    

    这是豆子入口

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/TestDB"></property>
        <property name="resourceRef" value="true"></property>
    </bean>
    
    
    
    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Co
    nnection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Ca
    nnot load JDBC driver class 'com.mysql.jdbc.Driver'
            at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(Dat
    aSourceUtils.java:82)
            at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:
    382)
            at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:45
    8)
            at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:46
    6)
            at com.businesscaliber.dao.Dao.getQueryForListMap(Dao.java:66)
            at com.businesscaliber.dao.MiscellaneousDao.getDefaultSucessStory(Miscel
    laneousDao.java:109)
            at com.businesscaliber.listeners.BusinessContextLoader.contextInitialize
    d(BusinessContextLoader.java:40)
            at org.apache.catalina.core.StandardContext.listenerStart(StandardContex
    t.java:3795)
            at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
    252)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
    .java:760)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:74
    0)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
    
            at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:831)
    
            at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:720
    )
            at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490
    )
            at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
            at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
    :311)
            at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
    eSupport.java:120)
            at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
    
            at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
            at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
    
            at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443
    )
            at org.apache.catalina.core.StandardService.start(StandardService.java:4
    48)
            at org.apache.catalina.core.StandardServer.start(StandardServer.java:700
    )
            at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
            at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
    Caused by: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driv
    er class 'com.mysql.jdbc.Driver'
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDat
    aSource.java:1136)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSo
    urce.java:880)
            at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(D
    ataSourceUtils.java:113)
            at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(Dat
    aSourceUtils.java:79)
            ... 30 more
    Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
            at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:164)
            at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDat
    aSource.java:1130)
    
    3 回复  |  直到 14 年前
        1
  •  5
  •   Igor luckyamit    11 年前

    这里没什么复杂的-您已经配置了MySQL,但是用于MySQL的JDBC驱动程序要么从未安装,要么从未放置在类路径中。

    忽略/禁用Spring、Hibernate、连接池,只需在JSP中或任意尝试这个Java代码即可。

    Class.forName("com.mysql.jdbc.Driver");
    

    在这项工作(例如文件在类路径中)之前,您将继续感到悲伤

        2
  •  2
  •   Ivan Bürcher    14 年前

    必须执行以下步骤:

    1)春豆配置 ----------------------

        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
        <property name="database" value="MYSQL" />
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
       </bean>
      </property>
     </bean>
    
     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
      scope="singleton">
      <property name="jndiName" value="jdbc/jpaExamples" />
      <property name="resourceRef" value="true" />
     </bean>
    
     <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
     </bean>
    
     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
     </bean>
    
     <tx:annotation-driven transaction-manager="transactionManager" />
    
     <!-- Definition of your beans -->
    
    </beans>
    

    2)web xml中数据源资源的定义: ——————————————————————————————————————————————————————————————————————————————————————————————————————————————-

     <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/jpaExamples</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
     </resource-ref>
    

    3)server.xml上下文中的资源定义 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————---

    <Context docBase="WebServiceRequestPersistence_Web3" path="/WebServiceRequestPersistence_Web3" debug="5" reloadable="true" crossContext="true" source="org.eclipse.jst.jee.server:WebServiceRequestPersistence_Web3">
     <Resource 
      name="jdbc/jpaExamples" 
      auth="Container"
      type="javax.sql.DataSource"
      maxActive="100" 
      maxIdle="30" 
      maxWait="10000"
      username="myUsername"
      password="myPassword" 
      driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost:3306/jpaExamples" 
     />
    </Context>
    

    4)将JDBC驱动程序jar复制到apache tomcat 6的lib中 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————-- 在Apache Tomcat 6安装的LIB子目录中复制MySQL Java连接器(例如:myql连接器-java5.1.6jar)。

        3
  •  1
  •   Michael Tarimo    12 年前

    如果您正在使用MyEclipse,并且试图从MyEclipse Tomcat运行它; 转到首选项(窗口>首选项),然后转到服务器>集成沙盒>MyEclipse Tomcat>路径 在Prepend to CalssPath下,添加jar。