How can I Initialize getJdbcTemplate()?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38

Thread: How can I Initialize getJdbcTemplate()?

  1. #1
    Join Date
    Sep 2009
    Posts
    24

    How can I Initialize getJdbcTemplate()?

    Task- Just need to select table data (one column also ok) from table1 and return as java.util.List

    ------------------------------
    integration-dao.xml
    -----------------------------

    Code:
    <bean id="cListDAO "
    class="com…..CListDAO "
    p:dataSource-ref="dataSource" 
    p:sqlLoader-ref="sqlLoader" /> 
    
    
    <bean id="sqlLoader"
    class="com.asi.integration.common.business.dao.sql .SQLLoader" />
    -------------------------
    integration-datasource.xml
    -------------------------
    Code:
    <bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="${my.jdbc.driverClassName}"
    p:jdbcUrl="${my.jdbc.url}"
    p:user="${my.jdbc.username}"
    password="${my.jdbc.password}"
    p:initialPoolSize="${my.jdbc.pool.init.size}"
    p:maxPoolSize="${my.jdbc.pool.max.size}"
    preferredTestQuery="${my.jdbc.preferredTestQuery }"
    p:idleConnectionTestPeriod="${my.jdbc.idleConnecti onTestPeriod}"/>
    -------------------------------
    integration-hibernate.xml
    --------------------------------
    Code:
    <!-- Hibernate SessionFactory for datasource -->
    <bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean"
    p:dataSource-ref="dataSource">
    
    <!-- tried by giving this also--><!--<property name="dataSource" ref="dataSource"></property> --> 
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    ${my.hibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.max_fetch_depth">3</prop>
    <prop key="hibernate.query.substitutions">
    true 1, false 0
    </prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property> 
    </bean>
    -------------------------
    SQLConstants.java
    -------------------------
    Code:
    public class SQLConstants {
    public static final String PACKAGE = ClassUtils.getPackageName(SQLConstants.class);
    public static final String C_NUMBERS_LIST =
    "CNumberList.sql";
    
    /**
    * Private constructor to prevent instantiation.
    */
    private SQLConstants() {
    super();
    }
    }
    -------------------------
    CNumberList.sql
    ----------------------
    Code:
     select * FROM table1

    ----------------------------
    CListDAO.java
    -----------------------
    Code:
    public class CListDAO extends JdbcDAO { 
    
    
    public List claims() {
    
    
    List li = getJdbcTemplate().queryForList(
    this.loadQuery(SQLConstants.PACKAGE, 
    SQLConstants.C_NUMBERS_LIST));
    
    Iterator iter = li.iterator(); 
    System.out.println("I want ===>>"+li.size());
    while(iter.hasNext()){
    System.out.println("I want->>>>"+iter.next());
    }
    
    return li; 
    }
    --------------------------
    JdbcDAO.java
    ----------------------------------
    Code:
    public class JdbcDAO extends JdbcDaoSupport implements IJdbcDAO {
    private SQLLoader _sqlLoader;
    
    ….
    
    protected String loadQuery(String packageName, String sqlName) {
    return _sqlLoader.loadQuery(packageName, sqlName);
    }
    ….
    }
    ----------------------------------------


    I am getting NullPointerException at
    Code:
     “ getJdbcTemplate().queryForList(
    this.loadQuery(SQLConstants.PACKAGE, 
    SQLConstants.C_NUMBERS_LIST));
    , I observed getJdbcTemplate() Is comming as null.

    How can i create instance of JdbcTemplate here ?
    Do I need to use sessionfactory , if yes , how can I wire up ?

    Please tell me what I missed above.

    Thanks in advance.

    ==========================
    Error:
    ===========================
    Code:
     java.lang.NullPointerException
    at com…….CListDAO.getRecords(ClaimsListDAO.java:32)
    at com……..runScheduledJob(GenerationScheduledJob.java :52)
    at com….common.jobs.AbstractScheduledJob.execute(Abst ractScheduledJob.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at org.springframework.util.MethodInvoker.invoke(Meth odInvoker.java:276)
    at org.springframework.scheduling.quartz.MethodInvoki ngJobDetailFactoryBean$MethodInvokingJob.executeIn ternal(MethodInvokingJobDetailFactoryBean.java:260 )
    at org.springframework.scheduling.quartz.QuartzJobBea n.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:2 02)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run (SimpleThreadPool.java:529)
    Last edited by java_dev; September 14th, 2009 at 02:58 AM.

  2. #2
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    Can any one help me in solving my problem ?

  3. #3
    Join Date
    May 2009
    Location
    Surrey, UK
    Posts
    285

    Re: How can I Initialize getJdbcTemplate()?

    If you had used [CODE] tags probably someone might be interested in reading your code...

  4. #4
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    Thanks for reply jcaccia.

    I modified my post with code tags, Can you help me in solving my problem?

    Please let me know if you need more details from my end.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How can I Initialize getJdbcTemplate()?

    Quote Originally Posted by java_dev View Post
    I modified my post with code tags, Can you help me in solving my problem?
    The point of the code tags is to preserve the formatting. If the code is unformatted, they won't help.

    I am always doing that which I cannot do, in order that I may learn how to do it...
    P. Picasso
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    Yes, I can understand, that the reason I changed as per your suggestion. I need help in solving my problem.

    Please help me.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How can I Initialize getJdbcTemplate()?

    Quote Originally Posted by java_dev View Post
    Yes, I can understand, that the reason I changed as per your suggestion.
    <sigh> Your code is not formatted, so putting it in code tags is pointless. jcaccia was asking for formatted (readable) code.

    I need help in solving my problem.
    Perhaps jcaccia might be more inclined to help if the code was formatted and readable...?

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    Apr 2007
    Posts
    425

    Re: How can I Initialize getJdbcTemplate()?

    are you planning on providing your own transaction management?
    ------
    If you are satisfied with the responses, add to the user's rep!

  9. #9
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    Thanks for reply Deliverance!
    No, I am not planning to providing any transaction management.

    Result List of claims() I will give to a job.

    I am also updating my recent try….

    I modified my Integration-dao.xml as follows. Still I am getting same exception, do I need to change any thing on CListDAO.java ?

    Code:
    	<bean id="claimNumberListDAO"
    		class="com.asi.integration.cc.jobs.activity.ClaimNumberListDAO"
    		p:dataSource-ref="dataSource" p:sqlLoader-ref="sqlLoader" 
    		p:jdbcTemplate-ref="jdbcTemplate"/>
    
    	<!-- The db JdbcTemplate class -->
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>

  10. #10
    Join Date
    Apr 2007
    Posts
    425

    Re: How can I Initialize getJdbcTemplate()?

    It's hard to say, I've never done it that way. I have always provided some level of transaction management independent of hibernate.

    Have you tested everything with an in memory database?

    Code:
    	
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    	destroy-method="close">
    	<property name="driverClass" value="org.hsqldb.jdbcDriver" />
    	<property name="jdbcUrl" value="jdbc:hsqldb:mem:test" />
    	<property name="user" value="sa" />
    	<property name="password" value="" />
    </bean>
    then add a session manager as a bean, and in your code, wire the session factory to one of your DAOs and try

    Code:
    SessionFactoryUtils.getSession(sessionFactory, true);
    ------
    If you are satisfied with the responses, add to the user's rep!

  11. #11
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    yes, I tested my “datasource connection” by hard coding sql statement. It is working.

    I am able to proceed further with above exception, I am facing another exception

    I tried using interface as follows


    ICListDAO.java
    Code:
    public interface IListDAO extends IJdbcDAO {
    
    	void setCListDAO(CListDAO cListDAO);
    
    }
    IJdbcDAO.java
    Code:
    public interface IJdbcDAO {
    
    }
    CGenerationScheduledJob.java
    Code:
    public class CGenerationScheduledJob extends
    		AbstractScheduledJob implements ICListDAO {
    
    	private CListDAO cListDAO;
    
    	public void setCListDAO(CListDAO cListDAO) {
    		this.cListDAO = cListDAO;
    	}
    
    	public void runScheduledJob() {
    		try {
    			cListDAO.claims();
    		
    }
    Integration-scheduler.xml
    Code:
    <bean id="activityGenerationJob"
    		class="com….jobs.activity.ClaimCenterActivityGenerationScheduledJob"		
    		p:hostNameUtils-ref="hostNameUtils" p:cListDAO-ref="cListDAO"/>
    Integration-dao.xml
    Code:
    	<bean id="cListDAO"
    		class="com..jobs.activity.CListDAO"
    		p:dataSource-ref="dataSource" p:sqlLoader-ref="sqlLoader"
    		p:jdbcTemplate-ref="jdbcTemplate">
    	</bean>
    
    	<!-- The db JdbcTemplate class -->
    	<bean id="jdbcTemplate"
    		class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    
    	</bean>
    I am getting exception, please help me.

    Error:
    Code:
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:324)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activityGenerationJob' defined in ServletContext resource [/WEB-INF/Integration-scheduler.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy1] to required type [com.....cc.jobs.activity.CListDAO] for property 'cListDAO'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy1] to required type [com....cc.jobs.activity.CListDAO] for property 'cListDAO': no matching editors or conversion strategy found
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:478)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)

  12. #12
    Join Date
    Apr 2007
    Posts
    425

    Re: How can I Initialize getJdbcTemplate()?

    Quote Originally Posted by java_dev View Post
    Error:
    Code:
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:324)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activityGenerationJob' defined in ServletContext resource [/WEB-INF/Integration-scheduler.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy1] to required type [com.....cc.jobs.activity.CListDAO] for property 'cListDAO'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy1] to required type [com....cc.jobs.activity.CListDAO] for property 'cListDAO': no matching editors or conversion strategy found
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:478)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    This is due to the fact that Spring is dealing with your beans in terms of interface so that it can use the Java proxy mechanism to return you a decorated class, while making it look the same to you who is using it. I've ran into these problems when trying to wire or pass a reference to a concrete type rather than interface.

    Take a look at what CListDAO if you are using @Autowired , and make sure that you pass the interface to your activityGenerationJob
    ------
    If you are satisfied with the responses, add to the user's rep!

  13. #13
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    Thanks , I resolved this issue, can you please tell what is this design/concept name ? I want to read more on this by googling.

  14. #14
    Join Date
    Sep 2009
    Posts
    24

    Re: How can I Initialize getJdbcTemplate()?

    And also can you please give the best place(url) from where I can read about spring with help of example(like java tutorial)?

  15. #15
    Join Date
    Apr 2007
    Posts
    425

    Re: How can I Initialize getJdbcTemplate()?

    The Spring documentation is a great resource and at some point, you will benefit greatly from reading through the entire thing
    http://static.springsource.org/sprin...nce/index.html



    The design concept you were looking for is Java proxying.

    http://java.sun.com/j2se/1.4.2/docs/...ion/proxy.html
    Last edited by Deliverance; September 17th, 2009 at 08:24 AM.
    ------
    If you are satisfied with the responses, add to the user's rep!

Page 1 of 3 123 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center