JDBC 調用Oracle預存程序返回結果集並獲得輸出參數

來源:互聯網
上載者:User

JDBC 調用Oracle預存程序返回結果集並獲得輸出參數:

 
  1. package xx.qq.app;  
  2.   
  3. import java.sql.CallableStatement;  
  4. import java.sql.Connection;  
  5. import java.sql.ResultSet;  
  6.   
  7. import org.springframework.beans.factory.BeanFactory;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  11.   
  12. /** 
  13.  * @author Jack Zhang Email:fish2-2@163.com 
  14.  * @date 2011-08-22 
  15.  */  
  16. public class AppTest {  
  17.       
  18.       
  19.     public static void main(String[] args) throws Exception {  
  20.         ApplicationContext context = new ClassPathXmlApplicationContext(  
  21.                 new String[] { "applicationContext.xml" });  
  22.         BeanFactory factory = (BeanFactory) context;  
  23.         ComboPooledDataSource dataSource = (ComboPooledDataSource) factory  
  24.                 .getBean("dataSource");  
  25.         Connection con = dataSource.getConnection();  
  26.           
  27.         CallableStatement st =con.prepareCall("{call package_user.PERSON_QUERY(?,?)}");  
  28.         //st.setString(1, "");   
  29.         st.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);  
  30.         st.registerOutParameter(2, oracle.jdbc.OracleTypes.INTEGER);  
  31.         st.execute();  
  32.         ResultSet rs = null;  
  33.         rs = (ResultSet)st.getObject(1);  
  34.         while(rs.next())  
  35.         {  
  36.             System.out.println(rs.getObject(1)+" "+rs.getObject(2));  
  37.         }  
  38.         int count =st.getInt(2);  //擷取輸出參數   
  39.         System.err.println(count);  
  40.         if (rs != null)  
  41.             rs.close();  
  42.         if (st != null)  
  43.             st.close();  
  44.         if (con != null)  
  45.             con.close();  
  46.     }  
  47. }  

 

 
  1. CREATE OR REPLACE PACKAGE package_user   AS  
  2. TYPE MY_RESULTSET_CURSOR IS REF CURSOR;  
  3. PROCEDURE PERSON_QUERY(RS OUT MY_RESULTSET_CURSOR,C OUT INTEGER);  
  4. END;  

 

 
  1. create or replace package body package_user AS  
  2.        PROCEDURE  PERSON_QUERY(RS OUT MY_RESULTSET_CURSOR,C OUT INTEGER)  
  3.         AS  
  4.              BEGIN  
  5.                   OPEN RS FOR select * from emp;  
  6.                   update A set ename='AAA';  
  7.                   SELECT COUNT(*) INTO C from emp;  
  8.         END PERSON_QUERY;  
  9. END;  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.