Announcement Announcement Module
Collapse
No announcement yet.
JDBCDAOSupport can't find dataSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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, 09:27 PM.

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            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

            Comment

            Working...
            X