Wednesday, May 22, 2013

Oracle IN OUT cursor with java

Following sample fully developed using
http://www.idevelopment.info/data/Programming/java/jdbc/PLSQL_and_JDBC/RefCursorExample.java
link

How to work with IN/OUT cursor with java
Connection Class
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

/**
 *
 * @author suresh_inova
 */
public class ConnectionDB {
    final static String driverClass    = "oracle.jdbc.driver.OracleDriver";
    Connection   con                   = null;


    /**
     * Construct a RefCursorExample object. This constructor will create an Oracle
     * database connection.
     */
    public ConnectionDB(String connectionURL, String userID, String userPassword) {

        try {

            System.out.print("  Loading JDBC Driver  -> " + driverClass + "\n");
            Class.forName(driverClass).newInstance();

            System.out.print("  Connecting to        -> " + connectionURL + "\n");
            this.con = DriverManager.getConnection(connectionURL, userID, userPassword);
            System.out.print("  Connected as         -> " + userID + "\n\n");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
   
    public Connection getConnection() {//TODO have to use singlenton
       return con;
    }
}

Ref Cursor sample with Oracle imports

import java.sql.Connection;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleResultSet;
import oracle.jdbc.OracleTypes;
import test.ConnectionDB;

/**
 *
 * @author suresh_inova
 */
public class RefCursorRBM {

    ConnectionDB conDb = null;
    Connection con = null;

    public RefCursorRBM() {
        ConnectionDB conDb = new ConnectionDB("jdbc:oracle:thin:@172.26.30.89:1521:TEST", "userName", "pwd");
        con = conDb.getConnection();
    }

    public void performRefCursor() {

        OracleCallableStatement oraCallStmt = null;
        OracleResultSet deptResultSet = null;

        try {
            oraCallStmt = (OracleCallableStatement) con.prepareCall("{call RBM_CUSTOM.DBT_GETCOLLECTIONS.GETMONCOLL(?,?,?,?,?)}");
            oraCallStmt.registerOutParameter(1, OracleTypes.CURSOR);
            oraCallStmt.registerOutParameter(1, OracleTypes.CURSOR);
            oraCallStmt.setString(2, "20130501");
            oraCallStmt.setString(3, "20130601");
            oraCallStmt.setString(4, "HEAD");
            oraCallStmt.setString(5, "GSM");
            oraCallStmt.execute();

            deptResultSet = (OracleResultSet) oraCallStmt.getCursor(1);

            while (deptResultSet.next()) {
                System.out.println(
                        " - " + deptResultSet.getString(1) + " - "
                        + deptResultSet.getString(2) + " - "
                        + deptResultSet.getString(3) + " - ");
            }

            oraCallStmt.close();
            closeConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Close down Oracle connection.
     */
    public void closeConnection() {

        try {
            System.out.print("  Closing Connection...\n");
            con.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
}

This site providing you to JAVA Language easy way. I think if you are beginner to JAVA refer this to implements you knowledge using only with very simplest codes.