Sample Illustrating Object Type Inheritance Support in Oracle9i JDBC Drivers

Table Of Contents 

Overview of the Sample Application 

Back To Top

This sample illustrates the object type inheritance support in Oracle9i JDBC Drivers. Object-type inheritance feature allows to create a new object type by extending another object type. The new object type is then a subtype of the object type from which it extends. The subtype automatically inherits all the attributes and methods defined in the supertype. The subtype can add attributes, methods and overload or override methods inherited from the supertype.

Sample Application Scenario

              Consider a Residential University in which all persons are categorized under Students, Teachers and Part-time Students. This model has four new object types. Person is a super type and its attributes are common to all persons. It has two sub types Student and Teacher. Teacher has additional attributes like courses taught, Salary and date of joining. Student has additional attributes like courses taken, grade and year of completion. A Part-time student is a sub-type of Student with some additional attributes such as No of hours, Start time and Batch code. All the sub-types have overridden methods for setting and retrieving the attributes.



In this sample the user can View/Edit person details.For viewing person details, the overridden getpersondetails() method is called which returns a String representation of the Object attributes. For updating person details, the overridden setpersondetails() method is called, which sets the attributes and internally calls the setaddress() method for setting the address.


This sample illustrates
  • Method Overloading: Here the method getPersonDetails() is overloaded. When invoked on Student Object, this method returns the courses taken, grade etc information apart from Name and Address information. When invoked on Teacher Object, this method returns the information like courses taught, Salary, Department apart from Name and Address.

  • Dynamic Method Dispatching: This is nothing but run-time polymorphism. When you invoke an overridden method, the implementation of the methods is searched in the current type and then upwards in the type hierarchy (type and super types of the type). In other words, if setAddress() is invoked from a Student Object, then method in Person Object is invoked, since there is no implementation of setAddress() in Student Object, but if the same method is called from a Teacher object the implementation in the Teacher Object is called.

  • Substituting Types in a Type Hierarchy: A base object type can hold any of its sub-types object i.e. Person is the base type and it can hold either a Student, Teacher or a Part-time Student object which are sub-types of Person.

Here is the code usage for using ObjectTypeInheritance feature of Oracle9i JDBC drivers. You can find more details of the code in ObjectTypeInheritanceSample.java file under src\oracle\otnsamples\oracle9ijdbc\objecttypeinheritance folder. Look into Description of Sample Files section for folder and file details.

public class ObjectTypeInheritanceSample {
.......................

/**
* This method gets the Id of the person who has been selected in the JTable
* and retrieves the corresponding person object from the PersonDetails table.
*/
public Person getPersonDetails(String p_id) {
   Person personObj = null;
   PreparedStatement pstmt = null;

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

   pstmt = m_conn.prepareStatement(" SELECT p.person "+
   " FROM PersonDetails p "+
   " WHERE p.person.Id=? ");

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


   // select the person object from PersonDetails table
   rset = pstmt.executeQuery ();

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

   // Retrieve the object and assign it to a person object, a person object
   // can hold all its sub-type objects (substitutability)
   personObj = (Person)rset.getObject(1);

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

}

/**
* This method retrieves the objects from the PersonDetails table and displays
* Id, Name and Object type of objects in a JTable.
*/
private void displayPersonDetails() {

   Person personObj;

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

   // Retrieve the objects from PersonDetails table
   rset = stmt.executeQuery("SELECT p.person FROM PersonDetails p");

   // Retrieve the object and assign it to a Person object, a Person object
   // can hold all its sub-type objects (substitutability)
   personObj = (Person)rset.getObject(1);

   // An object of Part-time Student will also be an instance of Student,
   // since Part-time Student is a sub-type of student. Hence the retrieved
   // object is first compared with a partimestudent and then with a
   // student(a student object will not be an instance of Part-time Student)

   // If the retrieved object is a Part-timeStudent
   if(personObj instanceof ParttimeStudent)
      objType=" P ";

   // If the retrieved object is a student
   else if(personObj instanceof Student)
      objType=" S ";

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


   // Get the Id
   Id = personObj.getId();
.....................

}

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

 

Notations used

This following notations are used through out this document

Notation

Description

<SAMPLE_HOME>

Folder where the ObjectTypeInheritanceSample 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.2.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.0.1 JDBC Drivers for use with JDK 1.2.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
  • Unjar the provided ObjectTypeInheritanceSample.jar using the following command 

    > jar xvf ObjectTypeInheritanceSample.jar

    Note: You will find jar.exe in <JAVA_HOME>\bin. Ensure <JAVA_HOME>\bin is present in your system path. 
    For setting up environment variables in different platforms, please refer environment set up readme document.


    This creates a folder
    ObjectTypeInheritanceSample with all the source files.

  • Edit ObjectTypeInheritanceSample\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

    =

    scott

    Password

    =

    tiger

NOTE: The Custom java classes which are generated by the JPublisher and included along with the sample use "SCOTT" schema. In case you use some other database user, then you need to generate the Custom java classes using JPublisher utility. Run the SQL script and then follow the instructions for generating custom java classes.

Database Setup

Back To Top
  • To create the required tables and populate it with sample data, run SQL*Plus or Server Manager, connect to your test database as scott/tiger and execute the install.sql script as shown below.
    SQL> @<SAMPLE_HOME>\sql\install.sql

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 ObjectTypeInheritanceSample.jws from the ObjectTypeInheritanceSample directory.
  • Now, select Run/Run ObjectTypeInheritanceSample.jpr from main menu which opens up the browser and runs the ObjectTypeInheritanceSample.

Running the application - From command line

Back To Top

          This section describes steps to run the application from console using JDK(version 1.2 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 <ORACLE_HOME>] 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

  • If JDK 1.4 is used, then ojdbc14.jar(downloadable from OTN) must be used. For all other JDK versions, classes12.jar must be used.

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

  • Execute the script file in Windows as follows
    D:\ObjectTypeInheritanceSample\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: classes12.jar or classes12.zip or ojdbc14.jar
  • . Also set the CLASSPATH to include Oracle9i SQLJ driver files: translator.jar and runtime12ee.jar. For more information on how to setup environment variables in different platforms, please refer environment set up readme document

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

  • Make sure that <JAVA_HOME>\bin is in the path. Also make sure that <ORACLE_HOME>\bin is in the classpath which contains sqlj.exe.
    Note :If JDK 1.4 is used, then ojdbc14.jar(downloadable from OTN) must be used. For all other JDK versions, classes12.jar must be used.

  • From the directory ObjectTypeInheritanceSample\src\oracle\otnsamples\oracle9ijdbc\objecttypeinheritance, compile all the java files using sqlj:
    Example:
     
       sqlj -d=. -status -ser2class *.sqlj *.java

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

    java oracle.otnsamples.oracle9ijdbc
    .objecttypeinheritance.ObjectTypeInheritanceSample

Description of Sample Files 

Back To Top

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


Directory
Files
Description

ObjectTypeInheritanceSample

ObjectTypeInheritanceSample.jws

The Oracle9i JDeveloper workspace file

ObjectTypeInheritanceSample.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

ObjectTypeInheritanceSample\src\oracle\otnsamples\oracle9ijdbc\objecttypeinheritance

ObjectTypeInheritanceSample.java

The main Source file for the Sample

Person.sqlj

The sqlj class file for mapping the SQL Object type Person_type.

Student.sqlj

The sqlj class file for mapping the SQL Object type Student_type.

Teacher.sqlj

The sqlj class file for mapping the SQL Object type Teacher_type.

ParttimeStudent.sqlj

The sqlj class file for mapping the SQL Object type Parttime_Student_type.

ObjectTypeInheritanceSample\sql

install.sql

This file creates the tables required by the sample and inserts sample data



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