Sample illustrating use of oracle.jdbc.OracleConnectionWrapper

Table Of Contents 

Overview of the Sample Application 

Back To Top

This sample illustrates the use of OracleConnectionWrapper (oracle.jdbc.OracleConnectionWrapper) class. Use of OracleConnectionWrapper prevents direct access to OracleConnection. Instead it wraps up the OracleConnection and routes the JDBC calls from user to OracleConnection. Since all the JDBC calls have to be made through the wrapper class, the business logic can be implemented at one place, thus implementing a single point of control and reducing code duplication.

Sample Application Scenario

             The sample application uses the database tables from oe schema of Oracle9i Database. This sample application demonstrates the use of oracle.jdbc.OracleConnectionWrapper, using an Order-Entry Application. The user can perform a maximum of 10 operations(query or update the status of an order). The number of operations performed is displayed to the user. When the user reaches the limit, any further operations are disabled and the user has to quit the application.


Here is the code usage for Connection Wrapper class. You can find more details of the code in CustomConnWrapper.java file under src/oracle/otnsamples/oracle9ijdbc/ConnectionWrapper folder. Look into Description of Sample Files section for folder and file details.

public class CustomConnWrapper extends oracle.jdbc.OracleConnectionWrapper {
.......................

// Constructor which takes OracelConnection which is to be wrapped
public CustomConnWrapper(OracleConnection toBeWrapped ) {
super(toBeWrapped);
}

// Method which intercepts createStatement call to underlying database
public Statement createStatement() throws SQLException {

// Business logic - Count the number of statement objects created
stmtCount++;

return super.createStatement();
}

public PreparedStatement prepareStatement(String sql)
throws SQLException{

// Count the number of PreparedStatement objects created
stmtCount++;

return super.prepareStatement(sql) ;
}

.......................

 

Notations used

This following notations are used through out this document

Notation

Description

<SAMPLE_HOME>

Folder where the ConnectionWrapperSample will be unzipped.

<JAVA_HOME>

Folder where JAVA is installed.

<JDBC_HOME>

Folder where the Oracle JDBC driver is installed.

<ORACLE_HOME>

Folder where the ORACLE installed.


Required Software

Back To Top

  • Oracle9i JDeveloper ( Note: Oracle9i JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here )
    or
    JDK1.4.x or above This can be downloaded from here .
  • Oracle9i Database or higher running SQL*Net TCP/IP listener. This can be downloaded from here .
  • Oracle9i v9.2 JDBC Drivers for use with JDK 1.4.x , downloadable from OTN site.
    If Oracle9i client is already installed on your system then this driver need not be downloaded separately.

Application Set-up and Configuration

Back To Top

  • For setting up environment variables in different platforms, please refer environment set up readme document.

  • Unjar the provided ConnectionWrapperSample.jar using the following command 

    > jar xvf ConnectionWrapperSample.jar

    Note: You will find jar.exe in <JAVA_HOME>\bin. Ensure <JAVA_HOME>\bin is present in your system path. 
    This creates a folder
    ConnectionWrapperSample with all the source files.

  • Edit ConnectionWrapperSample\Connection.properties file in your favorite editor. Change the HostName, Port, SID, UserName and Password to connect to your own database.

HostName

=

localhost

SID

=

ORCL

Port

=

1521

UserName

=

oe

Password

=

oe

Running the application using Oracle9i JDeveloper

Back To Top

This section describes the steps required in running this application using Oracle9i JDeveloper.

  • Open Oracle9iJDeveloper and use File/Open option to select the ConnectionWrapperSample.jws from the ConnectionWrapperSample directory.
  • Next, select Project/Make ConnectionWrapperSample.jpr from main menu.
    • Set CLASSPATH to Oracle9i JDBC drivers. It can be done as follows.
      • Select Project/Project settings which displays the Project settings window
      • The options will be displayed in a tree format. Go to the Library options under the current active coniguration.
      • To add Oracle9i JDBC libraries, Click New button which displays a window 'New Library'.
        • Give some name(Oracle JDBC14) in the LIbrary name field shown.
        • Click Edit button at CLASSPATH field which displays a new window.
        • Click 'Add Entry' which displays a File Chooser window.
          • Browse for class lbraries ojdbc14.jar ( In <ORACLE_HOME>/jdbc/lib of Oracle v9.2 Database or Client instalaltion )
      • Click on Select button
      • Click on OK button till you return to Libraries option.

    Note : Oracle9i JDeveloper must use JDK1.4 for compiling and running this sample
    So make sure that Oracle9i JDeveloper uses JDK1.4 for compiling and running this sample. In order to use JDK 1.4 from Oracle9i JDeveloper follow the steps below.

    • Select Project/Project settings which displays the Project settings window
    • The options will be displayed in a tree format. Go to the Library options under the current active coniguration.
    • If JDK1.4 is listed in the JDK versions combo box, then select that. If not, define a new J2SE version for JDK1.4 by providing the location of <JAVA_HOME>/bin/java.exe.

  • Now, select Run/Run ConnectionWrapperSample.jpr from main menu which opens up the browser and runs the ConnectionWrapperSample.

Running the application - From command line

Back To Top

          This section describes steps to run the application from console using JDK(version 1.4 or above) for Windows and Redhat Linux Advanced Server version 2.1. This application can be run either manually or using a script file.

Run the application using script file :

  • Make sure that the environment variables[<JAVA_HOME> and <JDBC_HOME> in the PATH] have been set before proceeding futher. For more information on how to setup these environment variables in different platforms, please refer environment set up readme document

  • Now the sample application can be directly run by just executing the script file: run.bat/run.sh from the command prompt, from ConnectionWrapperSample directory.

  • Execute the script file in Windows as follows
    D:\ConnectionWrapperSample\run

    For Redhat Linux Advanced Server version 2.1 environment, execute the script file as follows:
    $sh run.sh

Running the application manually:

  • Set CLASSPATH to include Oracle9i JDBC Driver file: ojdbc14.jar
  • . For more information on how to setup environment variables in different platforms, please refer environment set up readme document

  • Also add ConnectionWrapperSample directory where Connection.properties exists and the current directory to the CLASSPATH

  • Make sure that <JAVA_HOME>/bin is in the path.

  • From the directory ConnectionWrapperSample\src\oracle\otnsamples\oracle9ijdbc\connectionwrapper, compile all the java files using javac:
    Example:
     
       javac -d . *.java

  • Run the class file using java from  ConnetionWrapperSample\src\oracle\otnsamples\oracle9ijdbc\connectionwrapper\src directory
    Example:

    java oracle.otnsamples.oracle9ijdbc.connectionwrapper.ConnectionWrapperSample

Description of Sample Files 

Back To Top

The directory structure of the deliverable ConnectionWrapperSample.jar will be as shown below. ConnectionWrapperSample is the top level directory.


Directory
Files
Description

ConnectionWrapperSample

ConnectionWrapperSample.jws

The Oracle9i JDeveloper workspace file

ConnectionWrapperSample.jpr

The Oracle9i JDeveloper project file

connection.properties

This file has the details of the database connection parameters

run.bat

The batch file to compile and run the sample in Windows environment

run.sh

The shell script file to compile and run the sample in Linux environment

ConnectionWrapperSample\src\oracle\otnsamples\oracle9ijdbc\connectionwrapper

ConnectionWrapperSample.java

The Source file for the Sample

ConnectionWrapper.java

The Custom Connection Wrapper Class



Please enter your comments about this sample in the OTN Sample Code Discussion Forum.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy