/* SQL script needed */

package networking;

import java.sql.*;

import oracle.jdbc.driver.OracleCallableStatement;
import oracle.jdbc.driver.OracleTypes;


/* Could also use import oracle.jdbc.driver.*; instead of the last two above imports */
public class refcursor_out {

    public static void main(String[] args) {

        refcursor_out vTest = new refcursor_out();

        vTest.call_stmt();
    }

    void call_stmt() {

        try {
            DriverManager.registerDriver(
                new oracle.jdbc.driver.OracleDriver());

            Connection conn = DriverManager.getConnection(
// somehow oci8 does not return any results.
//                                  "jdbc:oracle:oci8:@127.0.0.1:1521:athena",
                                  "jdbc:oracle:thin:@127.0.0.1:1521:athena",
                                  "scott", "tiger");
            CallableStatement ocstmt;

            ocstmt = conn.prepareCall(
                "begin test_ref_cursor.test_ref_cursor(?,?); end;");

            ocstmt.setString(2, "select ename from emp");
            ocstmt.registerOutParameter(1, OracleTypes.CURSOR);
            ocstmt.execute();

            OracleCallableStatement tstmt;

            tstmt = (OracleCallableStatement) ocstmt;

            ResultSet cursor;

            cursor = tstmt.getCursor(1);

            // Use the cursor like a normal ResultSet
            while(cursor.next()) {
                System.out.println(cursor.getString(1));
            }
        } catch(Exception e) {}
    }
}