---
title: "Spring 2.5中调用Oracle存储过程"
author: "seven0_0"
date: "2010-08-20T11:04:59Z"
tags: []
categories: ["企业架构"]
slug: "spring-2-5-oracle"
draft: false
lastmod: "2010-08-20T11:04:59Z"
id: 742850
url: "https://sevenjava.iteye.com/blog/742850"
---

/\*\*调用存储过程  
  存储过程  
   create or replace procedure getselect(arg in varchar2,out\_result out sys\_refcursor)  
   is  
   begin  
    open out\_result for select * from user\_tab\_cols a where a.table\_name = arg;  
   end  getselect;  
   \*/

 public void getCallableStatementCallback(){  
  ApplicationContext context = new FileSystemXmlApplicationContext(  
  "src/database\_config.xml");  
  JdbcTemplate jt = new JdbcTemplate((DataSource) context.getBean("oracleDataSourceTest"));  
  // 测试用的方法  
  List relist = (List) jt.execute("call getselect(?,?)",  
    new CallableStatementCallback() {  
     public Object doInCallableStatement(CallableStatement cs)  
       throws SQLException, DataAccessException {  
      cs.setString(1, "ACCT");  
      cs.registerOutParameter(2,oracle.jdbc.driver.OracleTypes.CURSOR);  
      cs.execute();  
      ResultSet rs = (ResultSet) cs.getObject(2);  
      List result = new ArrayList();  
      while (rs != null && rs.next()) {  
       Map map = new HashMap();  
       map.put("TABLE\_NAME", rs.getString(1));  
       map.put("COLUMN\_NAME", rs.getString(2));  
       result.add(map);  
      }  
      rs.close();  
      return result; // 返回类型决定execute返回类型  
     }  
    });  
  for (int i = 0; i &lt; relist.size(); i++) {  
   Map rmap = (Map) relist.get(i);  
   System.out.println(rmap.get("TABLE\_NAME") + " "+ rmap.get("COLUMN\_NAME"));  
  }  
 }