[ Back | Previous | Next ]

How to retrieve and handle a cursor from Oracle database?

Package:
java.sql.*
Product:
JDK
Release:
1.1
Related Links:
General
General
General
General
CallableStatement
Comment:
CODE
SQL



/**
 *
 */
public void testGetMessagePartsAsCursor() throws SQLException
{
    String sql = "begin :1 := returnCursor(); end;";
    sql = "begin open ? for select part_data from tdoa_message_part; end;";
    java.util.Hashtable t = new java.util.Hashtable();

    for (int i = 0; i < 100; i++)
        {
        java.sql.CallableStatement call = getConnection().prepareCall(sql);
        call.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
        long lBegin = System.currentTimeMillis();
        long lEnd = 0;
        //System.out.println(" start= " + lBegin);
        Object key = new java.util.Date();
        call.execute();
        //
        //          OracleResultSet r = (OracleResultSet) call.getC(1);
        ResultSet cursor = ((OracleCallableStatement) call).getCursor(1);
        for (int rsCnt = 0; cursor.next(); rsCnt++)
            {
            //System.out.println("Iteration " + rsCnt + " Fetched " + rsCnt + " rows.");
            System.out.print(".");
        }
        cursor.close();
        call.close();
        lEnd = System.currentTimeMillis();
        //System.out.println(" end= " + lEnd + "\n dif=" + (lEnd - lBegin) + "ms.");
        t.put(key, new Long(lEnd - lBegin));
    }
    printResults(t);

}





/*
* SQL-CODE
*/
CREATE OR REPLACE PACKAGE test
AS
   TYPE myrctype  IS REF CURSOR RETURN  titeltype;   
    FUNCTION returnCursor
    RETURN myrctype;
END test;
/ 
CREATE OR REPLACE PACKAGE BODY test
AS
  FUNCTION returnCursor
    RETURN myrctype
    IS
      rc myrctype;      
BEGIN
        OPEN rc FOR SELECT   *
                    FROM     mydatabase;  
       RETURN rc;                    
      END returnCursor;
END test;
1