1. 程式人生 > >Spring如何使用JdbcTemplate呼叫儲存過程的三種情況

Spring如何使用JdbcTemplate呼叫儲存過程的三種情況

Spring的SimpleJdbcTemplate將儲存過程的呼叫進行了良好的封裝,下面列出使用JdbcTemplate呼叫Oracle儲存過程的三種情況: 

一、無返回值的儲存過程呼叫 

1、儲存過程程式碼:

create or replace procedure sp_insert_table(param1 in varchar2,param2 in varchar2) as   
   begin  
       insert into table MyTable (id,name) values ('param1 ','param2');  
   end sp_insert_table;  

2、JdbcTemplate呼叫該儲存過程程式碼:
  package com.dragon.test;   
  import org.springframework.jdbc.core.JdbcTemplate;   
  public class JdbcTemplateTest {   
    private JdbcTemplate jdbcTemplate;   
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {   
    this.jdbcTemplate = jdbcTemplate;   
    }   
    public void test(){   
       this.jdbcTemplate.execute("call sp_insert_table('100001')");   
    }   
  }   

二、有返回值的儲存過程(非結果集)

1、儲存過程程式碼:

create or replace procedure sp_select_table (param1 in varchar2,param2 out varchar2) as   
 begin select into param2 from MyTable where ID = param1 ; 
end sp_insert_table ; 

2、JdbcTemplate呼叫該儲存過程程式碼:

public void test() { 
  String param2Value = (String) jdbcTemplate.execute( 
     new CallableStatementCreator() { 
        public CallableStatement createCallableStatement(Connection con) throws SQLException { 
           String storedProc = "{call sp_select_table (?,?)}";// 呼叫的sql 
           CallableStatement cs = con.prepareCall(storedProc); 
           cs.setString(1, "p1");// 設定輸入引數的值 
           cs.registerOutParameter(2,OracleTypes.Varchar);// 註冊輸出引數的型別 
           return cs; 
        } 
     }, new CallableStatementCallback() { 
         public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { 
           cs.execute(); 
           return cs.getString(2);// 獲取輸出引數的值 
     } 
  }); 
} 

三、有返回值的儲存過程(結果集)

1、儲存過程程式碼:先建立程式包,因為Oracle儲存過程所有返回值都是通過out引數返回的,列表同樣也不例外,但由於是集合,所以不能用一般的引數,必須要用package:

create or replace package mypackage as
    type my_cursor is ref cursor;
    end mypackage;
2、儲存過程程式碼:可以看到,列表是通過把遊標作為一個out引數來返回的。  
create or replace procedure sp_list_table(param1 in varchar2,param2 out mypackage.my_cursor) is
    begin
    open my_cursor for select * from myTable;
    end sp_list_table;

3、JdbcTemplate呼叫該儲存過程程式碼:

public void test() { 
  List resultList = (List) jdbcTemplate.execute( 
     new CallableStatementCreator() { 
        public CallableStatement createCallableStatement(Connection con) throws SQLException { 
           String storedProc = "{call sp_list_table(?,?)}";// 呼叫的sql 
           CallableStatement cs = con.prepareCall(storedProc); 
           cs.setString(1, "p1");// 設定輸入引數的值 
           cs.registerOutParameter(2, OracleTypes.CURSOR);// 註冊輸出引數的型別 
           return cs; 
        } 
     }, new CallableStatementCallback() { 
        public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException { 
           List resultsMap = new ArrayList(); 
           cs.execute(); 
           ResultSet rs = (ResultSet) cs.getObject(2);// 獲取遊標一行的值 
           while (rs.next()) {// 轉換每行的返回值到Map中 
              Map rowMap = new HashMap(); 
              rowMap.put("id", rs.getString("id")); 
              rowMap.put("name", rs.getString("name")); 
              resultsMap.add(rowMap); 
           } 
           rs.close(); 
           return resultsMap; 
        } 
  }); 
  for (int i = 0; i < resultList.size(); i++) { 
     Map rowMap = (Map) resultList.get(i); 
     String id = rowMap.get("id").toString(); 
     String name = rowMap.get("name").toString(); 
     System.out.println("id=" + id + ";name=" + name); 
  } 
} 

作者:佇望碧落 出處:http://blog.csdn.net/cl05300629

<pre code_snippet_id="190768" snippet_file_name="blog_20140217_3_4197233" name="code" class="java" style="font-size: 14px; line-height: 25.200000762939453px;"><pre code_snippet_id="190768" snippet_file_name="blog_20140217_3_4197233">