获取java.lang.ClassNotFoundException:com.mysql.jdbc.Driver Exception

时间:2010-03-12 05:50:46

标签: spring tomcat

我在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 Entry

              

<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 个答案:

答案 0 :(得分:5)

这里没有什么复杂的 - 你已经配置了mysql,但你从未安装或从未放置在类路径中的mysql的jdbc驱动程序。

忽略/禁用spring,hibernate,连接池,只需在jsp或其他任何地方尝试这个java代码。

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

在此之前(例如文件在类路径中),你将继续悲伤

答案 1 :(得分:2)

您必须执行以下步骤:

1)Spring bean的配置 --------------------------------

    <?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中DataSource资源的定义: ------------------------------------------------

 <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中 -------------------------------------------------- ---- 将您的MySQL Java连接器(例如:mysql-connector-java-5.1.6.jar)复制到Apache Tomcat 6安装的lib子目录中。

答案 2 :(得分:1)

如果您正在使用MyEclipse,并且您正尝试从MyEclipse Tomcat运行它; 转到首选项(窗口&gt;首选项),然后转到服务器&gt;集成沙箱&gt; MyEclipse Tomcat&gt;路径 在press to calsspath下,添加jar。