<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hk2000c技術(shù)專欄

    技術(shù)源于哲學,哲學來源于生活 關(guān)心生活,關(guān)注健康,關(guān)心他人

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks

    這是總結(jié)以前使用spring調(diào)用Oracle存儲過程,并用cursor返回結(jié)果集的一個完整實例,希望能對大家有幫助。

    1. 創(chuàng)建表:

    代碼
    1. create table TEST_USERS    
    2. (    
    3.   USER_ID  VARCHAR2(10) not null,    
    4.   NAME     VARCHAR2(10) not null,    
    5.   PASSWORD VARCHAR2(20) not null    
    6. )  

     

    2. 創(chuàng)建存儲過程:

    代碼
    1. create or replace package display_users_package is    
    2.      type search_results is ref cursor;    
    3.      procedure display_users_proc(results_out out search_results, userId in test_users.user_id%type);    
    4. end display_users_package;    
    5.   
    6. create or replace package body display_users_package is    
    7.      procedure display_users_proc(results_out out search_results, userId in test_users.user_id%type)    
    8.           is    
    9.           begin    
    10.           if userId is not null then    
    11.               open results_out for select * from test_users where user_id like userId || '%';    
    12.           else    
    13.               open results_out for  select * from test_users;    
    14.           end if;    
    15.       end display_users_proc;    
    16. end display_users_package;  

     

    這個results_out是一個游標類型,用來返回查找的結(jié)果集。

    3. 完整實現(xiàn)代碼:

    代碼
    1. import java.sql.CallableStatement;   
    2. import java.sql.Connection;   
    3. import java.sql.ResultSet;   
    4. import java.sql.SQLException;   
    5. import java.util.ArrayList;   
    6. import java.util.HashMap;   
    7. import java.util.List;   
    8. import java.util.Map;   
    9.   
    10. import javax.sql.DataSource;   
    11.   
    12. import oracle.jdbc.OracleTypes;   
    13.   
    14. import org.springframework.dao.DataAccessException;   
    15. import org.springframework.jdbc.core.CallableStatementCallback;   
    16. import org.springframework.jdbc.core.CallableStatementCreator;   
    17. import org.springframework.jdbc.core.JdbcTemplate;   
    18.   
    19. import com.spring.stored.procedure.util.DataContextUtil;   
    20.   
    21. /**  
    22.  * @author Jane Jiao  
    23.  *  
    24.  */  
    25. public class SpringStoredProce {   
    26.        
    27.     public List<Map> execute(String storedProc, String params){   
    28.         List<Map> resultList = null;   
    29.         try{   
    30.             final DataSource ds = DataContextUtil.getInstance().getDataSource();   
    31.             final JdbcTemplate template = new JdbcTemplate(ds);   
    32.             resultList = (List<Map>)template.execute(new ProcCallableStatementCreator(storedProc, params),   
    33.                                                      new ProcCallableStatementCallback());   
    34.         }catch(DataAccessException e){   
    35.             throw new RuntimeException("execute method error : DataAccessException " + e.getMessage());   
    36.         }   
    37.          return resultList;   
    38.     }   
    39.        
    40.        
    41.     /**  
    42.      * Create a callable statement in this connection.  
    43.      */  
    44.     private class ProcCallableStatementCreator implements CallableStatementCreator {   
    45.         private String storedProc;   
    46.         private String params;   
    47.            
    48.        
    49.         /**  
    50.          * Constructs a callable statement.  
    51.          * @param storedProc                  The stored procedure's name.  
    52.          * @param params                      Input parameters.  
    53.          * @param outResultCount              count of output result set.  
    54.          */  
    55.         public ProcCallableStatementCreator(String storedProc, String params) {   
    56.             this.params = params;   
    57.             this.storedProc = storedProc;   
    58.         }   
    59.            
    60.         /**  
    61.          * Returns a callable statement  
    62.          * @param conn          Connection to use to create statement  
    63.          * @return cs           A callable statement  
    64.          */  
    65.         public CallableStatement createCallableStatement(Connection conn) {   
    66.             StringBuffer storedProcName = new StringBuffer("call ");   
    67.             storedProcName.append(storedProc + "(");   
    68.             //set output parameters   
    69.             storedProcName.append("?");   
    70.             storedProcName.append(", ");   
    71.                
    72.             //set input parameters   
    73.             storedProcName.append("?");   
    74.             storedProcName.append(")");   
    75.   
    76.             CallableStatement cs = null;   
    77.             try {   
    78.                 // set the first parameter is OracleTyep.CURSOR for oracel stored procedure   
    79.                 cs = conn.prepareCall(storedProcName.toString());   
    80.                 cs.registerOutParameter (1, OracleTypes.CURSOR);   
    81.                // set the sencond paramter   
    82.                 cs.setObject(2, params);   
    83.             } catch (SQLException e) {   
    84.                 throw new RuntimeException("createCallableStatement method Error : SQLException " + e.getMessage());   
    85.             }   
    86.             return cs;   
    87.         }   
    88.            
    89.     }   
    90.        
    91.     /**  
    92.      *   
    93.      * The ProcCallableStatementCallback return a result object,   
    94.      * for example a collection of domain objects.  
    95.      *  
    96.      */  
    97.     private class ProcCallableStatementCallback implements CallableStatementCallback {   
    98.            
    99.         /**  
    100.          * Constructs a ProcCallableStatementCallback.  
    101.          */  
    102.         public ProcCallableStatementCallback() {   
    103.         }   
    104.   
    105.         /**  
    106.          * Returns a List(Map) collection.  
    107.          * @param cs                       object that can create a CallableStatement given a Connection  
    108.          * @return resultsList             a result object returned by the action, or null  
    109.          */  
    110.         public Object doInCallableStatement(CallableStatement cs){   
    111.             List<Map> resultsMap =  new ArrayList<Map>();   
    112.             try {   
    113.                 cs.execute();    
    114.                 ResultSet rs = (ResultSet) cs.getObject(1);   
    115.                 while (rs.next()) {   
    116.                     Map<String, String> rowMap = new HashMap<String, String>();   
    117.                     rowMap.put("userId", rs.getString("USER_ID"));   
    118.                     rowMap.put("name", rs.getString("NAME"));   
    119.                     rowMap.put("password", rs.getString("PASSWORD"));   
    120.                     resultsMap.add(rowMap);   
    121.                 }      
    122.                 rs.close();   
    123.             }catch(SQLException e) {   
    124.                 throw new RuntimeException("doInCallableStatement method error : SQLException " + e.getMessage());   
    125.             }   
    126.             return resultsMap;   
    127.        }   
    128.     }   
    129. }   

     

    4. 測試代碼,在這里使用了Junit4測試:

    代碼
    1. import static org.junit.Assert.assertNotNull;    
    2. import static org.junit.Assert.assertTrue;    
    3.   
    4. import java.util.List;    
    5. import java.util.Map;    
    6.   
    7. import org.junit.After;    
    8. import org.junit.Before;    
    9. import org.junit.Test;    
    10.   
    11. /**   
    12.  * @author Jane Jiao   
    13.  *   
    14.  */    
    15. public class SpringStoredProceTest {    
    16.        
    17.    private SpringStoredProce springStoredProce;    
    18.   
    19.    /**   
    20.     * @throws java.lang.Exception   
    21.     */    
    22.    @Before    
    23.    public void setUp() throws Exception {    
    24.       springStoredProce = new SpringStoredProce();    
    25.    }    
    26.   
    27.    /**   
    28.     * @throws java.lang.Exception   
    29.     */    
    30.    @After    
    31.    public void tearDown() throws Exception {    
    32.       springStoredProce = null;    
    33.    }    
    34.   
    35.    /**   
    36.     * Test method for {@link com.hactl.listingframework.dao.SpringStoredProce#execute(java.lang.String, java.lang.String)}.   
    37.     */    
    38.    @Test    
    39.    public void testExecute() {    
    40.       final String storedProcName = "display_users_package.display_users_proc";    
    41.       final String param = "test";    
    42.       List<Map> resultList = springStoredProce.execute(storedProcName, param);    
    43.       assertNotNull(resultList);    
    44.       assertTrue(resultList.size() > 0);    
    45.       for (int i = 0; i < resultList.size(); i++) {    
    46.          Map rowMap = resultList.get(i);    
    47.          final String userId = rowMap.get("userId").toString();    
    48.          final String name = rowMap.get("name").toString();    
    49.          final String password = rowMap.get("password").toString();    
    50.          System.out.println("USER_ID=" + userId + "\t name=" + name + "\t password=" + password);    
    51.       }    
    52.           
    53.    }    
    54. }  

     

    5. 測試的輸出結(jié)果:

    代碼
    1. USER_ID=test1    name=aa    password=aa    
    2. USER_ID=test2    name=bb    password=bb    
    3. USER_ID=test3    name=cc    password=cc  
    posted on 2007-11-16 17:31 hk2000c 閱讀(3569) 評論(2)  編輯  收藏 所屬分類: JMS

    評論

    # re: spring調(diào)用Oracle存儲過程,并返回結(jié)果集的完整實例 2008-09-08 23:43 mu
    寫點注釋啊,這么看著你自己很明白,別人云里來霧里去的  回復  更多評論
      

    # re: spring調(diào)用Oracle存儲過程,并返回結(jié)果集的完整實例 2010-09-26 16:39 asdtiang
    學習標記下  回復  更多評論
      

    主站蜘蛛池模板: 亚洲黄片手机免费观看| 嘿嘿嘿视频免费网站在线观看| 日韩在线视频线视频免费网站| 叮咚影视在线观看免费完整版| 免费毛片在线播放| 国产精品免费αv视频| 亚洲成人网在线播放| 亚洲国产人成精品| 日韩人妻无码精品久久免费一 | 国产美女无遮挡免费视频网站| 亚洲国产精品白丝在线观看| 男女交性永久免费视频播放| 午夜爽爽爽男女免费观看影院| 国产亚洲人成A在线V网站| free哆啪啪免费永久| sihu国产精品永久免费| 亚洲香蕉久久一区二区| 国产精品久久久亚洲| 日韩毛片无码永久免费看| 国内精品免费在线观看| 狠狠热精品免费观看| 中文字幕亚洲色图| 中文字幕久久亚洲一区| 噜噜嘿在线视频免费观看| 久久国产高潮流白浆免费观看| 亚洲自偷精品视频自拍| 国产偷国产偷亚洲高清日韩| 青青青国产在线观看免费 | 成人福利在线观看免费视频| 亚洲国产精品乱码在线观看97 | AV在线播放日韩亚洲欧| 男男AV纯肉无码免费播放无码| 亚洲中文字幕乱码AV波多JI| 亚洲成AV人片在| 亚洲一本大道无码av天堂| 黄瓜视频高清在线看免费下载| 国产偷国产偷亚洲高清在线| 亚洲人成电影院在线观看| 亚洲欧洲日韩不卡| 亚洲熟妇无码另类久久久| 亚洲精品国产V片在线观看|