请教用Spring的jdbc封装数据时,怎么样取得dataSource???在线等! ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml");Datasource ds = (Datasource) ctx.getBean("dataSource"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 spring的注入有三种方法,setter注入,构造函数和静态工厂方法一般用setter注入,在listTopMsg这个方法所在的类中加入DataSource的get set方法在你的spring配置文件中<bean id="" class=""> <ref bean name="dataSource"/></bean> 我用这种办法已试过了,,测试说得不到连接,,我debug时看到dataSource里有信息,我写了个测试类,是这样的: public void testListTopMsg() throws DataAccessException { ApplicationContext context = new FileSystemXmlApplicationContext("dataAccessContext.xml"); messageDaoImpl = (MessageDaoImpl)context.getBean("msgDao"); dataSource = (DataSource)context.getBean("dataSource"); messageDaoImpl.setDataSource(dataSource); List actualReturn =messageDaoImpl.listTopMsg("tr0001"); Iterator iter = actualReturn.iterator(); hs = new HashMap(); lastValue = new HashMap(); while (iter.hasNext()) { CompCgsMessage seq = (CompCgsMessage) iter.next(); hs.put(seq.getFieldName().trim(), seq); System.out.println(seq.getFieldName().trim()); } }当运行到 processRow(ResultSet rs)时,报org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactoryorg.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory, cause: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] CLI0106E 连接关闭。 SQLSTATE=08003哪位大侠帮我看看是怎么回事呢?? myth822(枫红一刀) 说的,我都已做啦 不使用spring的JDBC模板试试可不可以 myth822(枫红一刀) ,没懂你的意思. 单独使用JDBC的dataSource的getConnection 那应该没问题啊,我数据库是好好的啊.直接用jdbc不可能用问题啊 很简单啊,只要你在Spring的配置文件中把datasource设置好,然后在程序中直接用ApplicationContext context = new FileSystemXmlApplicationContext("dataAccessContext.xml");调用就行了,你的这个是没有问题的,但是下面就不用在getBean("datasource")了,没有必要,程序中从来就不会有和数据库有关系的代码,都已经用ORM搞定了,只要你的DAO继承HibernateDaoSupport就可以了。 楼主的做法没有什么问题啊,他也没有用hibernate 我不想用hibernate,想支接用Spring的jdbc.. Spring 和Hibernate 整合的问题 求一个ajax调用java方法得例子 请教如何将HashMap里的数据读出,拼成字符串,如001,002,003 J2EE 高分请教:远程访问jboss server下的MBbean struts error resource配置问题 struts vs spring Help me ! 一个新手J2EE中的各种名词的疑惑。 String dateStr = "26 Aug 2002 05:58:55 -0400"; 对不起,我要实现的是,JSP被访问一次后,把文件删掉,仍能访问该页面,能不能做到 规则引擎的问题 散分100
一般用setter注入,在listTopMsg这个方法所在的类中加入DataSource的get set方法
在你的spring配置文件中
<bean id="" class="">
<ref bean name="dataSource"/>
</bean>
我写了个测试类,是这样的:
public void testListTopMsg() throws DataAccessException {
ApplicationContext context = new FileSystemXmlApplicationContext("dataAccessContext.xml");
messageDaoImpl = (MessageDaoImpl)context.getBean("msgDao"); dataSource = (DataSource)context.getBean("dataSource");
messageDaoImpl.setDataSource(dataSource);
List actualReturn =messageDaoImpl.listTopMsg("tr0001"); Iterator iter = actualReturn.iterator();
hs = new HashMap();
lastValue = new HashMap();
while (iter.hasNext()) {
CompCgsMessage seq = (CompCgsMessage) iter.next();
hs.put(seq.getFieldName().trim(), seq);
System.out.println(seq.getFieldName().trim());
}
}当运行到 processRow(ResultSet rs)时,报
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory, cause:
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] CLI0106E 连接关闭。 SQLSTATE=08003
哪位大侠帮我看看是怎么回事呢??
没懂你的意思.
调用就行了,你的这个是没有问题的,但是下面就不用在getBean("datasource")了,没有必要,程序中从来就不会有和数据库有关系的代码,都已经用ORM搞定了,只要你的DAO继承HibernateDaoSupport就可以了。