Announcement Announcement Module
Collapse
No announcement yet.
Problem with Spring JDBC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Spring JDBC

    Hi All,
    I am complete newbie to Spring.I am trying to use Spring 3,SQL Server and Tomcat 6.
    I am not getting any results using Spring JDBC.
    I have retrieved results in a non spring way and the datasource works fine.
    This is in my 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:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	
    	<bean id="researchDao" class="com.aexp.forms.dao.ResearchDao">
    		<property name="dataSource">
    			<ref local="dataSource"/>
    		</property>
    	</bean>
    	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName">
    			<value>java:comp/env/jdbc/dsnForms</value>
    		</property>
    	</bean>
    	
    </beans>
    This is in my DAO.
    Code:
    public class ResearchDao extends JdbcDaoSupport{
    private JdbcTemplate jdbcTemplate;
    public int getResearchCount() {
        System.out.println(this.jdbcTemplate.queryForInt("select count(*) from t_forms_research_data"));
        return this.jdbcTemplate.queryForInt("select count(*) from t_forms_research_data");
    }
    }
    This is in my web.xml
    Code:
    <resource-ref>
    		<description>RainForest DB Connection</description>
    		<res-ref-name>jdbc/dsnForms</res-ref-name>
    		<res-type>javax.sql.DataSource</res-type>
    		<res-auth>Container</res-auth>
    	</resource-ref>
    Any help is appreciated.
    Thanks

  • #2
    Remove the jdbcTemplate and use getJdbcTemplate().

    Comment


    • #3
      Thanks for the rpely Marten.
      I replace it with the getJdbcTemplte() method.
      Code:
      public int getResearchCount() {
          	return this.getJdbcTemplate().queryForInt("select count(*) from t_forms_research_data");
          }
      But I still get a null pointer exception when I try to get results from the method like this
      Code:
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      ResearchDao r =(ResearchDao)context.getBean("researchDao");
      System.out.println("count="+r.getResearchCount());

      Comment


      • #4
        For starters you shouldn't create a new applicationcontext, let the app server do that and inject your dependencies.

        Post the stacktrace because you are leaving out the interesting information.

        Comment


        • #5
          Originally posted by springfullinto View Post
          But I still get a null pointer exception when I try to get results from the method like this
          Are there any previous errors to you NullPointerException?

          Originally posted by Marten Deinum View Post
          For starters you shouldn't create a new applicationcontext, let the app server do that and inject your dependencies.

          Post the stacktrace because you are leaving out the interesting information.
          Marten was quicker than me, just pust the StackTrace for more details.

          Comment


          • #6
            Thnaks Marten. I am complete newbie to Spring. I am trying it out so I can see if it is suitable for one of our apps. So please dont mind me asking questions.
            I am trying to print the log statements related to the spring jdbc with this
            Code:
            log4j.rootLogger=DEBUG,R
            log4j.appender.R=org.apache.log4j.RollingFileAppender
            log4j.appender.R.File= C:/Program Files/Apache Software Foundation/Tomcat 6.0/logs/marketingformsLog.log
            log4j.appender.R.layout=org.apache.log4j.PatternLayout
            log4j.appender.R.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
            log4j.logger.org.springframework.jdbc.core=DEBUG
            log4j.logger.org.springframework.beans.factory=DEBUG
            I have included this in web.xml.
            Code:
            <context-param>
            		<param-name>log4jConfigLocation</param-name>
            		<param-value>/WEB-INF/classes/log4j.properties</param-value>
            	</context-param>
            <listener>
            		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
            	</listener>
            I have no luck with that.
            For now this is in my stack trace.
            Code:
            http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
            11 Mar 2010 10:23:05,079 DEBUG PluggableSchemaResolver:118 - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
            11 Mar 2010 10:23:05,094 DEBUG DefaultBeanDefinitionDocumentReader:87 - Loading bean definitions
            11 Mar 2010 10:23:05,110 DEBUG XmlBeanDefinitionReader:185 - Loaded 2 bean definitions from location pattern [applicationContext.xml]
            11 Mar 2010 10:23:05,110 DEBUG ClassPathXmlApplicationContext:461 - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@b6421: org.springframework.beans.factory.support.DefaultListableBeanFactory@1781288: defining beans [researchDao,dataSource]; root of factory hierarchy
            11 Mar 2010 10:23:05,110 DEBUG ClassPathXmlApplicationContext:735 - Unable to locate MessageSource with name 'messageSource': using default [[email protected]08]
            11 Mar 2010 10:23:05,110 DEBUG ClassPathXmlApplicationContext:759 - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@ce623f]
            11 Mar 2010 10:23:05,110  INFO DefaultListableBeanFactory:532 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1781288: defining beans [researchDao,dataSource]; root of factory hierarchy
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:214 - Creating shared instance of singleton bean 'researchDao'
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:424 - Creating instance of bean 'researchDao'
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:498 - Eagerly caching bean 'researchDao' to allow for resolving potential circular references
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:214 - Creating shared instance of singleton bean 'dataSource'
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:424 - Creating instance of bean 'dataSource'
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:498 - Eagerly caching bean 'dataSource' to allow for resolving potential circular references
            11 Mar 2010 10:23:05,110 DEBUG DefaultListableBeanFactory:1444 - Invoking afterPropertiesSet() on bean with name 'dataSource'
            11 Mar 2010 10:23:05,110 DEBUG JndiTemplate:150 - Looking up JNDI object with name [java:comp/env/jdbc/dsnForms]
            11 Mar 2010 10:23:05,110 DEBUG JndiObjectFactoryBean:111 - Located object with JNDI name [java:comp/env/jdbc/dsnForms]
            11 Mar 2010 10:23:05,125 DEBUG DefaultListableBeanFactory:452 - Finished creating instance of bean 'dataSource'
            11 Mar 2010 10:23:05,125 DEBUG DefaultListableBeanFactory:1444 - Invoking afterPropertiesSet() on bean with name 'researchDao'
            11 Mar 2010 10:23:05,125 DEBUG DefaultListableBeanFactory:452 - Finished creating instance of bean 'researchDao'
            11 Mar 2010 10:23:05,125 DEBUG DefaultListableBeanFactory:241 - Returning cached instance of singleton bean 'dataSource'
            11 Mar 2010 10:23:05,125 DEBUG ClassPathXmlApplicationContext:786 - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [[email protected]3c1]
            11 Mar 2010 10:23:05,125 DEBUG DefaultListableBeanFactory:241 - Returning cached instance of singleton bean 'lifecycleProcessor'
            11 Mar 2010 10:23:05,125 DEBUG ClassPathXmlApplicationContext:292 - Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@b6421: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@b6421: startup date [Thu Mar 11 10:23:05 EST 2010]; root of context hierarchy]
            11 Mar 2010 10:23:05,125 DEBUG DefaultListableBeanFactory:241 - Returning cached instance of singleton bean 'researchDao'
            java.lang.NullPointerException
            	com.aexp.forms.dao.ResearchDao.getResearchCount(ResearchDao.java:56)
            	org.apache.jsp.test_jsp._jspService(test_jsp.java:76)
            	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
            	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
            	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
            	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
            	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            Last edited by springfullinto; Mar 11th, 2010, 09:45 AM.

            Comment


            • #7
              Hello All,

              Any suggestions that would help me??

              Thanks

              Comment


              • #8
                Do you have any row in the database table?

                Comment

                Working...
                X