-
September 11th, 2009, 12:24 PM
#1
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.
-
September 14th, 2009, 02:30 AM
#2
Re: How can I Initialize getJdbcTemplate()?
Can any one help me in solving my problem ?
-
September 14th, 2009, 02:34 AM
#3
Re: How can I Initialize getJdbcTemplate()?
If you had used [CODE] tags probably someone might be interested in reading your code...
-
September 14th, 2009, 02:59 AM
#4
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.
-
September 14th, 2009, 07:57 AM
#5
Re: How can I Initialize getJdbcTemplate()?
Originally Posted by java_dev
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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 14th, 2009, 08:17 AM
#6
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.
-
September 14th, 2009, 09:34 AM
#7
Re: How can I Initialize getJdbcTemplate()?
Originally Posted by java_dev
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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 14th, 2009, 10:45 AM
#8
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!
-
September 14th, 2009, 11:41 AM
#9
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>
-
September 15th, 2009, 09:03 AM
#10
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!
-
September 16th, 2009, 04:29 AM
#11
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)
-
September 16th, 2009, 08:51 AM
#12
Re: How can I Initialize getJdbcTemplate()?
Originally Posted by java_dev
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!
-
September 17th, 2009, 01:27 AM
#13
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.
-
September 17th, 2009, 01:29 AM
#14
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)?
-
September 17th, 2009, 08:20 AM
#15
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|