Вы находитесь на странице: 1из 8

A white paper on Java Database Interaction Mechanism

Java Database Interaction Mechanism Using Oracle Reference Cursors


By

Ravi Kumar Buragapu

buragapuravi@yahoo.com Mobile: +91-9845718842 Abstract: This document describes on a unique mechanism and process that enable to make use of Oracle references cursors in Java Database programming in developing highly scalable business applications with greater performance.

About the Author: Ravi Kumar Buragapu is a Technical Lead in Wipro Technologies. His interest in IT led him from his Ph.D. in Electronics to objectoriented programming, and then to distributed computing and software architecture.

This document is confidential. The information contained herein is not to be distributed, revealed or disseminated outside to any company or any other party without the prior expressed consent and written permission of the Author, buragapuravi@yahoo.com

Ravi Kumar B

Page 1

1/20/2005

A white paper on Java Database Interaction Mechanism

CONTENTS

Introduction Purpose Synopsis

..3

..3 ....3 ...4

Database Interaction Mechanism Conclusion

......8

Ravi Kumar B

Page 2

1/20/2005

A white paper on Java Database Interaction Mechanism

Introduction
The conventional approaches in performing Database operations in Java through Statement and Prepared Statement etc. are not scalable when they require performing complex Database operations on huge data. Even though the standard database interaction mechanisms uses the connection pooling, the connection objects are not persistent, this leads creation of the Prepared Statement in each Database request by sending the complete SQLs and their associated parameters to the Database. In order to provide solutions to the aggressive business needs it is requiring process large and complex Database operations through huge Databases. The bets solution to the emerging business needs which require to perform huge data transfer through complex Database operations will be through Oracle Stored Procedure with Callable Statements. This approach resolves the performance issues, but the Stored Procedures are not capable of returning the data that can be understandable by Java.

Purpose
The purpose of this paper is to describe various the mechanisms that enable to make use of Oracle references cursors in Java Database Interaction Mechanism.

Synopsis
Reference cursors offer the ability to pass the arguments to procedures and functions inform of a cursor variable. This approach significantly increases the flexibility and scalability of the applications that need to perform huge and complex Database operations and data validations. In order to make use of the power of Oracle Ref. cursors in Java Database programming it require a mechanism that allows the Java to understand the ref. cursors that are being returned by the Oracle.

Ravi Kumar B

Page 3

1/20/2005

A white paper on Java Database Interaction Mechanism

This generic approach allows the developers to explore, use and feel the power of Oracle ref. cursors and the tricky logic in using them in Java Database programming. This approach will give higher efficiency to the applications by increasing the performance with minimal Database errors. This suggested mechanism is independent of Application servers, Database interaction APIs and connection pooling mechanisms.

Mechanism
This section describes on how to define the reference cursors (in a Java understandable way) and the way of implementing this mechanism in Java. Oracle ref cursors allow you to define queries as stored procedures in the Database. They can be used in java by executing a call through a callable statement to the stored procedure and casting the returned ref. cursors to ResultSet. Using the Reference cursors is a bit tricky. The following is the approach that allows using the Reference cursors in Java Database programming. Here is an example of a Stored Procedure using Ref Cursor. /* * Package Specification */
CREATE OR REPLACE PACKAGE REF_CURSOR_PROC AS

// Declare reference cursor

TYPE CURSOR_TYPE IS REF CURSOR;

// register all the IN and OUT parameters, // Note: To avoid the sensitivity of java drivers declare the OUT parameters // after the IN parameters
PROCEDURE getMenu ( PRODUCT_TYPE PRODUCT_CATEGORY PRICE IN RESULTS_MENU ); End REF_CURSOR_PROC; / IN VARCHAR2, IN VARCHAR2, VARCHAR2, OUT CURSOR_TYPE

Ravi Kumar B

Page 4

1/20/2005

A white paper on Java Database Interaction Mechanism

/* * Declare Package Body */


CREATE OR REPLACE PACKAGE BODY REF_CURSOR_PROC AS

/* * Sequence of parameter declaration and signature must be same as * Package specification. */


PROCEDURE getSubMenu ( PRODUCT_TYPE PRODUCT_CATEGORY PRICE RESULTS_SUB_MENU ) AS BEGIN IN VARCHAR2, IN VARCHAR2, IN VARCHAR2, OUT CURSOR_TYPE

/* * Opening the reference cursor is very simple by stating a simple open * Statement followed by FOR to hold the out put of the SQL query. */
OPEN RESULTS_SUB_MENU FOR SELECT PMD.fkmenuid, MM.menuname, MM.cid, MM.SID, MM.hotflag, PMD.amount, PMD.parentmenuid FROM packagemenudetails PMD, menumaster MM WHERE PMD.fkmenuid = MM.menuid AND (MM.expiry_date IS NULL OR MM.expiry_date > SYSDATE) AND PMD.parentmenuid = menu_id AND PMD.fkpackageid = package_id AND UPPER (PMD.fklanguageid) LIKE language_id AND UPPER (MM.fkhandsetid) LIKE handset_id ORDER BY PMD.menuitemsorder; END getSubMenu; END REF_CURSOR_PROC; /

Ravi Kumar B

Page 5

1/20/2005

A white paper on Java Database Interaction Mechanism

Below is the java program that describes on how to use and implement the Oracle reference cursors in Java Database programming.
import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import oracle.jdbc.driver.*; public class RefCursor { public static void main(String[] args) throws SQLException, ClassNotFoundException { ResultSet cursor = null; Connection con = null; CallableStatement cstmt = null; try{ con = createConnection(); cstmt = con.prepareCall ("{call RVIDEO_PROC.GET_SUBMENU(?,?,?,?,?)}"); cstmt.setString(1, "LG RD2030"); cstmt.setString(2, "EN"); cstmt.setString(3, "2"); cstmt.setString(4, "45"); /* * This is the way to register the OUT parameter. */ cstmt.registerOutParameter(5, OracleTypes.CURSOR); cstmt.execute(); /* * Below is the tricky mechanism to get the ref cursor from Oracle * Stored Procedure. * The most important issue is casting. Casting the Oracle cursor into * java.sql.ResultSet. * When the Oracle Ref Cursor got caste to javas ResultSet, developers * can do all normal ResultSet operations as required. */ cursor = ((OracleCallableStatement)cstmt).getCursor(5); while (cursor.next ()) { System.out.println ("MENUID : "+cursor.getString(1)); System.out.println ("MENUNAME : "+cursor.getString(2)); : "+cursor.getString(3)); System.out.println ("CID System.out.println ("SID : "+cursor.getString(4)); System.out.println ("AMOUNT : "+cursor.getString(6)); System.out.println ("-----------------------------------"); } } catch(Exception e){ System.out.println("SQL Error : "+e.getMessage()); }

Ravi Kumar B

Page 6

1/20/2005

A white paper on Java Database Interaction Mechanism finally { try { if (cursor != null) { cursor.close(); cursor = null; } if (cstmt != null) { cstmt.close(); cstmt = null; } if (con != null) { con.close(); con = null; } }catch (Exception ex) { System.out.println("Error in closing : "+ex.getMessage()); } } } // Static method to get the connection. public static Connection createConnection() throws SQLException, ClassNotFoundException { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@192.168.1.55:1521:nseinfo", "rvideoserver", "rvideoserver"); return conn; } }

There is another mechanism to cast the Oracle Reference cursor into Java ResultSet.

ResultSet rsObj = (ResultSet) CallableStatement.getObject( int Index); Ex. ResultSet rsObj = (ResultSet) cstmt.getObject(4);

Ravi Kumar B

Page 7

1/20/2005

A white paper on Java Database Interaction Mechanism

Conclusion
The cast to OracleCallableStatement is superfluous. A simple getCursor(5) does the trick. This way of returning a cursor is totally and utterly Oracle-specific and thus proprietary. We ended up writing our own JDBC-layer in order to encapsulate this difference to other JDBC-drivers. Even the Oracle thin and Merant-driver does it the regular way, i.e. getResultSet().The best way to tackle this problem is not to use FUNCTIONS, but PROCEDURES and to specify the last parameter as an OUT cursor. E.g. that's the way the Oracle thin driver expects it. "The another big advantage here is that the oracle developers can provide ready made queries to the java developers, so the java developers do not have to know a lot of sql". Working on Oracle, the stored procedures are the best option because are more efficient than execute sql statements. On the other side, for a java developer is more comfortable work with stored functions (that returns the ref cursor) than work with stored procedures. But an Oracle developer prefers working with procedures.

Ravi Kumar B

Page 8

1/20/2005

Вам также может понравиться