首先我们要用Spring 与 Jdbc 调用存储过程
step1 : Spring 与jdbc 整合 原理介绍
我们选用什么数据源呢在这里我们选用c3p0 数据连接池(当然这个连接池也是相对比较成熟的)
添加c3p0.jar 包
step 2: 配置数据源
<!-- c3p0 JDBC DataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" /> <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.0.108:1521:orcl" /> <property name="user" value="xxxx" /> <property name="password" value="xxx" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="3"/> <property name="initialPoolSize" value="1"/> </bean>
step3 : 配置Spring 与 Jdbc整合
<!-- JDBC Support --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"><ref bean="dataSource"/></property> </bean>
step4 : 使用jdbcTemplate 使用Spring 的IOC 依赖注入
<!-- 报表系统 --> <bean id="sqwsReportDao" class="com.xxx.xxxxx.dao.bbxt.impl.SqwsReportDAOImpl"> <property name="sessionFactory" ref="sessionFactory"></property> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean>
最后在程序中使用JdbcTemplate
/**
* 调用存储过程
* 2011-7-26
* @author liuqing
* @param procedureName
* @param year
* @return void
*/
public void call(final String procedureName,final int year) {
this.jdbcTemplate.execute(new ConnectionCallback() {
@Override
public Object doInConnection(Connection conn)
throws SQLException,DataAccessException {
Log.info(this.getClass() +
" JDBC year Call:" + procedureName);
CallableStatement call = conn.prepareCall(procedureName);
call.setInt(1, year);
call.execute();
return null;
}
});
}
方法调用
/**
* 方法调用代码
* 2011-7-26
* @author liuqing
*/
public void runReportProcedure(final String procedureName
,final int year) {
this.call("{ call " + procedureName + "(?)}",year);
}