spring 获取 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2430771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Exception
提问by Vicky
I am getting Following Exception while configuring the Connection Pool in Tomcat
在 Tomcat 中配置连接池时出现以下异常
This is Context.xml
这是 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>
This is Bean Entry
这是 Bean 入口
<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.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)
回答by MJB
Nothing complex here - you have mysql configured, but the jdbc driver for mysql either you never installed or never placed in the classpath.
这里没有什么复杂的 - 您已经配置了 mysql,但是用于 mysql 的 jdbc 驱动程序要么您从未安装过,要么从未放置在类路径中。
Ignore/disable spring, hibernate, connection pools and just try this java code in a jsp or whatever.
忽略/禁用 spring、hibernate、连接池,然后在 jsp 或其他任何东西中尝试这个 java 代码。
Class.forName("com.mysql.jdbc.Driver");
Until that works (eg file is in class path), you will continue to have sadness
直到成功(例如文件在类路径中),您将继续感到悲伤
回答by Ivan Bürcher
You must do the following steps:
您必须执行以下步骤:
1) Configuration of Spring beans --------------------------------
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) Definition of DataSource resource in web-xml: ------------------------------------------------
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) Definition of Resource in Context of server.xml --------------------------------------------------
3)server.xml Context中Resource的定义 --------------------------------------- -----------
<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) Copy of JDBC driver jar into lib of Apache Tomcat 6 ------------------------------------------------------Copy your MySQL Java connector (for example: mysql-connector-java-5.1.6.jar) in the lib sub-directory of Apache Tomcat 6 installation.
4) 将 JDBC 驱动程序 jar 拷贝到 Apache Tomcat 6 的 lib 中 --------------------- -----------------将您的 MySQL Java 连接器(例如:mysql-connector-java-5.1.6.jar)复制到 Apache Tomcat 6 安装的 lib 子目录中。
回答by Michael Tarimo
If you are using MyEclipse and you are trying to run it from MyEclipse Tomcat; Go to preferences (Window > Preferences), then Servers > Integrated Sandbox > MyEclipse Tomcat > Paths Under prepend to calsspath, add the jar.
如果您正在使用 MyEclipse 并且您正尝试从 MyEclipse Tomcat 运行它;转到首选项(Window > Preferences),然后 Servers > Integrated Sandbox > MyEclipse Tomcat > Paths 在 prepend to calsspath 下,添加 jar。

