http://www.chinaunix.net/jh/26/109123.html
够详细!!

解决方案 »

  1.   

    存储过程中,在包pack下有个function是这样定义的:
    type ResultSet is ref cursor;          
    function get_data(
    id_num    in   number,
    rs1       out  ResultSet,
    rs2       out  ResultSet
    )return number;
    在调用的时候这样做:import java.sql.*;
    import oracle.jdbc.OracleTypes;..
    String sql = "{ ? = call pack.get_data(?, ?, ?) }";
    CallableStatement proc = conn.prepareCall(sql);
    proc.registerOutParameter(1, Types.Integer);
    //下面指定第三个输出参数的类型是数据集。因为java.sql.Types中没有CURSOR,所以要用oracle.jdbc.OracleTypes中的值
    proc.registerOutParameter(3, OracleTypes.CURSOR);   
    proc.registerOutParameter(4, OracleTypes.CURSOR);
    int id = 1;
    proc.setInt(2, id);
    proc.execute();
    int returnValue = proc.getInt(1);
    ResultSet rs1 = (ResultSet)proc.getObject(3);
    ResultSet rs2 = (ResultSet)proc.getObject(4);
      

  2.   

    /*
     * This sample shows how to call a PL/SQL function that opens
     * a cursor and get the cursor back as a Java ResultSet.
     */import java.sql.*;
    import java.io.*;// Importing the Oracle Jdbc driver package makes the code more readable
    import oracle.jdbc.driver.*;class RefCursorExample
    {
      public static void main (String args [])
           throws SQLException
      {
        // Load the driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());    // Connect to the database
        // You can put a database name after the @ sign in the connection URL.
        Connection conn =
          DriverManager.getConnection ("jdbc:oracle:oci7:@", "scott", "tiger");    // Create the stored procedure
        init (conn);    // Prepare a PL/SQL call
        CallableStatement call =
          conn.prepareCall ("{ ? = call java_refcursor.job_listing (?)}");    // Find out all the SALES person
        call.registerOutParameter (1, OracleTypes.CURSOR);
        call.setString (2, "SALES");
        call.execute ();
        ResultSet rset = (ResultSet)call.getObject (1);    // Dump the cursor
        while (rset.next ())
          System.out.println (rset.getString ("ENAME"));
      }  // Utility function to create the stored procedure
      static void init (Connection conn)
           throws SQLException
      {
        Statement stmt = conn.createStatement ();    stmt.execute ("create or replace package java_refcursor as " +
      "  type myrctype is ref cursor return EMP%ROWTYPE; " +
      "  function job_listing (j varchar2) return myrctype; " +
      "end java_refcursor;");    stmt.execute ("create or replace package body java_refcursor as " +
      "  function job_listing (j varchar2) return myrctype is " +
      "    rc myrctype; " +
      "  begin " +
      "    open rc for select * from emp where job = j; " +
      "    return rc; " +
      "  end; " +
      "end java_refcursor;");
      }
    }
      

  3.   

    public class ExecuteProcedure {
    public ExecuteProcedure(){}
    public void execute(){
    try {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=csmDB","sa","753");
    String sql=null;
    CallableStatement state=null;
    ResultSet result;

    // state=con.prepareCall("execute testProc ?,?");
    // state.setString(1,"001");
    // state.setString(2,"csm");
    // state.execute();

    // state=con.prepareCall("execute getName ?,?");
    // state.registerOutParameter(2,Types.VARCHAR);
    // state.setString(1,"001");
    // state.execute();
    // System.out.println(state.getString(2));


    //下面这段代码不能正确运行,应为读出的数据为nul,实际上应该是
    //001的名称
    // state=con.prepareCall("execute getNameOf001 ?");
    // state.registerOutParameter(1,Types.VARCHAR);
    // state.execute();
    // int count=0;
    // System.out.println("count="+(count=state.getMetaData().getColumnCount()));
    // for(int i=0;i<count;i++){
    // System.out.println("columnName "+(i+1)+" is "+state.getMetaData().getColumnName(i+1));
    // }
    // System.out.println(state.getObject(1));
    // ResultSet result=(ResultSet)state.getObject(1);
    // while(result.next()){
    // System.out.println(result.getString(1));
    // } state=con.prepareCall("execute getTestCount");
    state.execute();
    result=state.getResultSet();
    while(result.next()){
    System.out.println("count="+result.getObject(1));
    }// state=con.prepareCall("execute getAllDataOfTest");
    // state.execute();
    // result=state.getResultSet();
    // int count=state.getMetaData().getColumnCount();
    // for(int i=0;i<count;i++){
    // System.out.print(state.getMetaData().getColumnName(i + 1)+"\t");
    // }
    // while(result.next()){
    // for(int i=0;i<count;i++){
    // System.out.print(result.getObject(i+1)+"\t");
    // }
    // System.out.println();
    // }
    }
    catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("OK");
    }
    public static void main(String[] args) {
    ExecuteProcedure test=new ExecuteProcedure();
    test.execute();
    }
    }