先写一个最简单的存储过程
CREATE OR REPLACE procedure zend_update_album(
i_id number,
i_artist varchar2,
i_title varchar2)
is begin
update album set artist = i_artist,title = i_title where id = i_id;
commit;
end;
/
然后在Spring框架中去调用,这里首先用jdbctemplate来调用。
1.新建一个JDBCTemplateDao类
package test;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class JDBCTemplateDao
{
private JdbcTemplate template = null;
//应该通过xml中配置直接可以得到数据源,不过在测试中无法得到
private DataSource dataSource = null;
/**
* 获得数据源
* @author tony
* @return
*/
public DataSource getDataSource()
{
return dataSource;
}
/**
* 通过IoC容器注入依赖
*
* @param dataSource
*/
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
}
/**
* 初始化函数
*
* @param dataSource
*/
public void init()
{
//template = new JdbcTemplate(dataSource);
}
public List SpringStoredProce(String storedProc){
List resultList = null;
try{
//测试中直接连接数据库
DriverManagerDataSource ds=new DriverManagerDataSource();
ds.setDriverClassName( "oracle.jdbc.driver.OracleDriver");
ds.setUrl( "jdbc:oracle:thin:@172.20.4.144:1521:develop");
ds.setUsername( "ppcuser");
ds.setPassword( "ppcuser");
System.out.println("ds:"+ds);
template = new JdbcTemplate(ds);
//jdbctemplate直接调用存储过程
template.execute(new ProcCallableStatementCreator(storedProc),
new ProcCallableStatementCallback());
}catch(DataAccessException e){
throw new RuntimeException("execute method error : DataAccessException " + e.getMessage());
}
return resultList;
}
public JdbcTemplate getTemplate() {
return template;
}
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
}
2.实现CallableStatementCreator接口
package test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import org.springframework.jdbc.core.CallableStatementCreator;
/**
* 实现CallableStatementCreator接口
* @author tony
*/
public class ProcCallableStatementCreator implements CallableStatementCreator {
private String storedProc;
/**
* 传入的参数操作
*/
public ProcCallableStatementCreator(String storedProc) {
this.storedProc = storedProc;
}
/**
*连接存储过程
*/
public CallableStatement createCallableStatement(Connection conn) {
StringBuffer storedProcName = new StringBuffer("call ");
//根据创建的存储过程中的参数个数,确定?的数量
storedProcName.append(storedProc + "(");
storedProcName.append("?");
storedProcName.append(", ");
storedProcName.append("?");
storedProcName.append(", ");
storedProcName.append("?");
storedProcName.append(")");
CallableStatement cs = null;
try {
cs = conn.prepareCall(storedProcName.toString());
//参数赋值
// album表id=id的数据artist和title改为artist和title。
int id = 2;
String artist = "中国";
String title = "测试";
cs.setInt(1, id);
cs.setString(2, artist);
cs.setString(3, title);
} catch (SQLException e) {
throw new RuntimeException("createCallableStatement method Error : SQLException " + e.getMessage());
}
return cs;
}
}
3.实现CallableStatementCallback接口
package test;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.CallableStatementCallback;
/**
* 实现CallableStatementCallback接口
* @author RobertTao
*
*/
public class ProcCallableStatementCallback implements CallableStatementCallback {
public ProcCallableStatementCallback() {
}
/**
* 如果存储过程有返回的值,在这里操作,并返回
*/
public Object doInCallableStatement(CallableStatement cs){
List resultsMap = null;
try {
cs.execute();
}catch(SQLException e) {
throw new RuntimeException("doInCallableStatement method error : SQLException " + e.getMessage());
}
return resultsMap;
}
}
4.最后直接写个test去测试一下
package test;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method
System.out.println("start" );
JDBCTemplateDao jdbcTemlate = new JDBCTemplateDao();
jdbcTemlate.SpringStoredProce("zend_update_album");
System.out.println("end" );
}
}
如果在Spring下,则直接可以取得数据库,只需在xml中配置如下
<bean id="jdbcTemplateDao" class="test.dao.JDBCTemplateDao">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource"/>
bean>
那么在jdbcTemplateDao中就可以根据get方法直接得到datasource。当然真要在那里测试跑起来,用action来调用还是要做蛮多事的,我就搞了一下午,一直出错,到晚上才发现在action里竟然把service搞错了,对自己无语。