Announcement Announcement Module
Collapse
No announcement yet.
Cannot load JDBC driver class 'com.mysql.jdbc.Driver' Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

    hey everyone, i'm following the 'Developing a Spring Framework MVC application step-by-step' tutorial for 2.5 and have gotten to the persistence layer. i am receiving the following error when i try to access my app. i know my machine recognizes the mysql driver as i am able to connect to mysql in other projects. the mysql-connector-java-5.1.7-bin.jar file is in my WEB-INF/lib folder as well as an external jar in my eclipse project. my tests are also running fine so i know it's creating tables and inserting data. any thoughts?? all of the other files are straight from the tutorial but let me know if you wanted to see anything else.

    error message
    exception

    org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransa ctionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:583)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

    root cause

    org.springframework.transaction.CannotCreateTransa ctionException: Could not open JDBC Connection for transaction; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    org.springframework.jdbc.datasource.DataSourceTran sactionManager.doBegin(DataSourceTransactionManage r.java:238)
    org.springframework.transaction.support.AbstractPl atformTransactionManager.getTransaction(AbstractPl atformTransactionManager.java:374)
    org.springframework.transaction.interceptor.Transa ctionAspectSupport.createTransactionIfNecessary(Tr ansactionAspectSupport.java:263)
    org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:101)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:89)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    $Proxy0.getProducts(Unknown Source)
    springapp.web.InventoryController.handleRequest(In ventoryController.java:33)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:48)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:875)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

    root cause

    org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    org.apache.commons.dbcp.BasicDataSource.createData Source(BasicDataSource.java:1136)
    org.apache.commons.dbcp.BasicDataSource.getConnect ion(BasicDataSource.java:880)
    org.springframework.jdbc.datasource.DataSourceTran sactionManager.doBegin(DataSourceTransactionManage r.java:200)
    org.springframework.transaction.support.AbstractPl atformTransactionManager.getTransaction(AbstractPl atformTransactionManager.java:374)
    org.springframework.transaction.interceptor.Transa ctionAspectSupport.createTransactionIfNecessary(Tr ansactionAspectSupport.java:263)
    org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:101)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:89)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    $Proxy0.getProducts(Unknown Source)
    springapp.web.InventoryController.handleRequest(In ventoryController.java:33)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:48)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:875)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)

    root cause

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1387)
    org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1233)
    java.lang.ClassLoader.loadClassInternal(Unknown Source)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Unknown Source)
    org.apache.commons.dbcp.BasicDataSource.createData Source(BasicDataSource.java:1130)
    org.apache.commons.dbcp.BasicDataSource.getConnect ion(BasicDataSource.java:880)
    org.springframework.jdbc.datasource.DataSourceTran sactionManager.doBegin(DataSourceTransactionManage r.java:200)
    org.springframework.transaction.support.AbstractPl atformTransactionManager.getTransaction(AbstractPl atformTransactionManager.java:374)
    org.springframework.transaction.interceptor.Transa ctionAspectSupport.createTransactionIfNecessary(Tr ansactionAspectSupport.java:263)
    org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:101)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.interceptor.ExposeInvocati onInterceptor.invoke(ExposeInvocationInterceptor.j ava:89)
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:204)
    $Proxy0.getProducts(Unknown Source)
    springapp.web.InventoryController.handleRequest(In ventoryController.java:33)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:48)
    org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:875)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    applicationContext.xml
    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
        <!-- the parent application context definition for the springapp application -->
    
        <bean id="productManager" class="springapp.service.SimpleProductManager">
            <property name="productDao" ref="productDao"/>
        </bean>
    
        <bean id="productDao" class="springapp.repository.JdbcProductDao">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <bean id="propertyConfigurer" 
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
    
        <bean id="transactionManager" 
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <aop:config>
            <aop:advisor pointcut="execution(* *..ProductManager.*(..))" advice-ref="txAdvice"/>
        </aop:config>
    
        <tx:advice id="txAdvice">
            <tx:attributes>
                <tx:method name="save*"/>
                <tx:method name="*" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    
    </beans>

  • #2
    root cause
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    Driver is not on class path.

    Comment


    • #3
      hey vinod,

      i noticed that error and thought that was the root cause. the mysql connector folder is in the CLASSPATH variable on my local machine. it is also noted as a referenced library in eclipse and i included it in the WEB-INF/lib folder of my project. do i need to do something else to include the driver on the classpath?

      also, just to reiterate, when the 'dbTests' run via the build command in the tutorial, they are able to connect to the database and drop/create tables as well as load data. that's why i'm confused when i get this error when i try to access http://localhost:8080/springapp/hello.htm
      Last edited by nickleli; Dec 27th, 2008, 10:27 AM.

      Comment


      • #4
        Though you mentioned that jar is available in 'WEB-INF/lib' but somehow server can't find it in classpath while Eclilpse can and thats why working fine. So look for some clues there.

        Comment


        • #5
          thanks vinod.

          i'm not sure why tomcat needed this but i simply copied the mysql connector jar in the catalina_home/lib directory and the app works fine. i've double checked in my deployed app that the mysql connector is in the app's lib folder so why can't tomcat resolve the file there? none of the other external jar's that the tutorial called out needed to be placed in cataline_home/lib to work...

          Comment


          • #6
            have you found the cause? thanks!

            hello i have the same error, however, my problem is not solved when i put mysql-connector jar in tomcat/lib directory, have you found out the cause the error? thanks a lot!

            Comment


            • #7
              when i had that problem with mysql i had to reference the driver in context.xml in META-INF in the app.

              Comment


              • #9
                <?xml version="1.0" encoding="UTF-8"?>
                <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


                <!--Album DAO Mapping -->
                <bean id="capacityDAO" class="testSpring.CapacitySpringDao">

                <property name="jdbcTemplate">
                <ref bean="jdbcTemplate" />
                </property>
                </bean>


                <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
                <constructor-arg>
                <ref bean="dataSource" />
                </constructor-arg>
                </bean>

                <bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/SpringDaoTestDatabase" />
                <property name="username" value="root" />
                <property name="password" value="password" />
                </bean>

                </beans>


                this gives me the following error:


                INFO: Loading XML bean definitions from class path resource [springdao-test.xml]
                11 Jul, 2011 1:15:20 AM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
                INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@4f5ad5c6: defining beans [capacityDAO,jdbcTemplate,dataSource]; root of factory hierarchy
                11 Jul, 2011 1:15:20 AM org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry destroySingletons
                INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@4f5ad5c6: defining beans [capacityDAO,jdbcTemplate,dataSource]; root of factory hierarchy
                Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'capacityDAO' defined in class path resource [springdao-test.xml]: Cannot resolve reference to bean 'jdbcTemplate' while setting bean property 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'jdbcTemplate' defined in class path resource [springdao-test.xml]: Cannot resolve reference to bean 'dataSource' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'dataSource' defined in class path resource [springdao-test.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessExceptions (1) are:
                PropertyAccessException 1: org.springframework.beans.MethodInvocationExceptio n: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
                at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:328)
                at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:106)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1325)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1086)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:517)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
                at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 95)
                at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
                at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:292 )
                at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:194)
                at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
                at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:913)
                at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:464)
                at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
                at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:93)
                at testSpring.CapacitySpringDaoTest.main(CapacitySpri ngDaoTest.java:11)
                Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'jdbcTemplate' defined in class path resource [springdao-test.xml]: Cannot resolve reference to bean 'dataSource' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'dataSource' defined in class path resource [springdao-test.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessExceptions (1) are:
                PropertyAccessException 1: org.springframework.beans.MethodInvocationExceptio n: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
                at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:328)
                at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:106)
                at org.springframework.beans.factory.support.Construc torResolver.resolveConstructorArguments(Constructo rResolver.java:630)
                at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:148)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1003)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:907)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
                at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 95)
                at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
                at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:292 )
                at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:194)
                at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:322)
                ... 15 more
                Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'dataSource' defined in class path resource [springdao-test.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessExceptions (1) are:
                PropertyAccessException 1: org.springframework.beans.MethodInvocationExceptio n: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1361)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1086)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:517)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
                at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 95)
                at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
                at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:292 )
                at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:194)
                at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:322)
                ... 27 more
                Caused by: org.springframework.beans.PropertyBatchUpdateExcep tion; nested PropertyAccessExceptions (1) are:
                PropertyAccessException 1: org.springframework.beans.MethodInvocationExceptio n: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
                at org.springframework.beans.AbstractPropertyAccessor .setPropertyValues(AbstractPropertyAccessor.java:1 02)
                at org.springframework.beans.AbstractPropertyAccessor .setPropertyValues(AbstractPropertyAccessor.java:5 8)
                at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1358)
                ... 35 more
                please help.

                Comment


                • #10
                  This can be solved by adding the following entry to your pom.xml file.

                  <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>5.1.6</version>
                  </dependency>

                  Originally posted by vinod.singh View Post
                  Driver is not on class path.

                  Comment

                  Working...
                  X