Results 1 to 6 of 6

Thread: JDBCDAOSupport can't find dataSource

  1. #1
    Join Date
    May 2012
    Posts
    9

    Default JDBCDAOSupport can't find dataSource

    Hi,

    I am new to Spring and am trying to setup JDBCDAOSupport in my mvc project to connect to a mysql backend. I am receiving the following error message:

    Code:
    java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
    I have configured my /webapp/WEB-INF/spring/appServlet/servlet-context.xml with the following:

    Code:
    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
    		<beans:property name="url" value="${jdbc.url}" />
    		<beans:property name="username" value="${jdbc.username}" />
    		<beans:property name="password" value="${jdbc.password}" />
    	</beans:bean>
    	
    	<context:property-placeholder location="classpath:jdbc.properties"/>
    Should I even be modifying the servlet-context.xml or should I create my own xml file? When I created the mvc project in STS I just went ahead and modified this one. If I do need to create a new one should I just put it under appServlet?

    Anyways onto the main problem. Another part of maven and spring is how should I go about adding the jdbc jar. When I go to my spring install folder under

    C:\Program Files\springsource\sts-2.9.1.RELEASE\plugins

    I see the org.springframework.jdbc_3.1.1.RELEASE jar, when I try and add this to the classpath I still receive the error. Should I add the dependency through maven? I would like to understand this aspect of spring and maven as I am new to it, but I am slightly confused on whether I should add it as a dependency or a jar in my build path.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    Hello Luca

    Have you read the Spring Core Reference documentation?

    java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerD ataSource
    If you have the project based on Maven, be sure you have the Spring JDBC dependency declared
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    May 2012
    Posts
    9

    Default

    I did declare the dependency in my pom.xml:

    Code:
    <dependency> <groupId>org.springframework</groupId> <artifactId>org.springframework.jdbc</artifactId> <version>3.1.1.RELEASE</version> </dependency>
    When I edit my pom.xml using the Maven editor I don't get an error stating that it can't find the jar file.

    I have the following class declaration and its complaining that the import for org.springframework.jdbc can't be resolved.

    Code:
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class JdbcRegisterTest extends JdbcDaoSupport implements TestDAO {
    	 
    }

    I'm looking at the servlet-context.xml again and its stating that it can't find the jar:

    Code:
    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
    		<beans:property name="url" value="${jdbc.url}" />
    		<beans:property name="username" value="${jdbc.username}" />
    		<beans:property name="password" value="${jdbc.password}" />
    	</beans:bean>
    The console is stating the following:

    Code:
    Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
    Last edited by luca3784; May 14th, 2012 at 08:27 PM.

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    Hello

    Code:
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    public class JdbcRegisterTest extends JdbcDaoSupport implements TestDAO {
    	 
    }
    About testing + Spring, I suggest you strongly read the Testing chapter in Spring Framework Reference Documentation. You have valuable annotations in your favor!

    I'm looking at the servlet-context.xml again and its stating that it can't find the jar:
    In such file you have declared the dataSource bean?, BTW how your Test class is getting and reading the xml files configurations?

    BTW about <artifactId>org.springframework.jdbc</artifactId> I think is wrong, from where you took such definition?
    I think should be spring-jdbc, check these results


    Could you post the complete error stack trace?

    BTW, why you are using DriverManagerDataSource? If my memory doesnt fail me, it is not good for production, check its API for more information
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  5. #5
    Join Date
    May 2012
    Posts
    9

    Default

    I changed the artifact id and it is now able to find the dependency. Do you suggest I just use JDBCTemplate instead? I went off of this http://www.mkyong.com/spring/spring-...port-examples/

    He has an example of using JdbcDaoSupport.

  6. #6
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    I changed the artifact id and it is now able to find the dependency.
    Good to know now work.

    Do you suggest I just use JDBCTemplate instead?
    I went off of this http://www.mkyong.com/spring/spring-...port-examples/
    He has an example of using JdbcDaoSupport.
    Such template is good

    The problem could be the DriverManagerDataSource, Check the Spring Core reference and in the forum, there are other implementations recommended for production. I used to work with C3PO, furthermore you must consider DB pool connections

    HTH
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •