ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
Asked Answered
M

3

22

I was trying to do some basic database operations. I'm using STS 3.2.0, Apache Tomcat 7.

My dispatcher-servlet.xml includes:

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

Apache Tomcat fails miserably with thousands of exceptions, starting with this one :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource

And these are my library definitions

EDIT 1: I also downloaded 3.0.3.RELEASE version of the jar, and its still the same.

FINAL EDIT It seems, you should add your external jar files manually to your deployment directory as well (well if you don't use any extension that'd do that for you)

Manutius answered 10/5, 2013 at 8:18 Comment(2)
Is that class actually in the jar file (open it with winzip or expand it in eclipse). If not then you have the wrong jar. If it is there then you are not picking it up when you start your app.Pallua
@DaveRlz I think it is in the jar. I can import and directly use its contents on my regular java files, it just does not seem to exist while my webapp is being started.Manutius
C
52

Try to add to POM.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
Caras answered 10/5, 2013 at 8:56 Comment(4)
I've regenerated my pom.xml file and added jdbc dependency, but result is same.Manutius
does your war file contain that jar in WEB-INF/lib folder?Caras
well at least now I understand why I should use extensions like maven. The problem was, even though I added those jar to my project, they simply didn't exist in my WEB-INF/lib folder. I'll add these result to my original post, and select yours as an answer. Thanks!Manutius
check mvnrepository.com/artifact/org.springframework/spring-jdbc for latest versionAldric
B
0

I think you should import selected jdbc jar into web app library.

Bertine answered 10/5, 2013 at 8:37 Comment(0)
K
0

if none of the above solution doesn't work, then manually add spring-jdbc jar file to webapp/WEB-INF/lib.

Krusche answered 14/6, 2018 at 19:15 Comment(2)
Try to expand on your answer a bitShredding
download spring-jdbc-5.0.2.RELEASE.jar and copy it, in webapp/WEB-INF/lib. this jar version may vary for your project. so download jar version which is suitable for your projectKrusche

© 2022 - 2024 — McMap. All rights reserved.