Oracle8i JPublisher User's Guide
Release 2 (8.1.6)

Part Number A81357-01

Library

Product

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Example: Using Classes Generated for Object Types

This section illustrates an example of how you can use the classes that JPublisher generates for object types. Suppose you have defined a SQL object type that contains attributes and methods. You use JPublisher to generate a <name>.sqlj and a <name>Ref.java file for the object type. To enhance the functionality of the Java class generated by JPublisher, you can extend the class. After translating and/or compiling the classes, you can use them in a program. For more information on this topic, see "Using Classes JPublisher Generates for Object Types".

The following steps demonstrate the scenario described above. In this case, you define a RationalO SQL object type that contains numerator and denominator attributes and several methods to manipulate rational numbers. After using JPublisher to generate the JPubRationalO.sqlj and a RationalORef.java files, you provide a file, RationalO.java, that enhances the functionality of the JPubRationalO class by extending it. After compiling the necessary files, you use the classes in a test file to test the performance of the RationalO.java class.

The sections following the steps list the contents of the files the steps mention.

  1. Create the SQL object type RationalO. "Listing of RationalO.sql to Create the Object Type" contains the code for the RationalO.sql file.

  2. Use JPublisher to generate Java classes (a JPubRationalO.sqlj file and a RationalORef.java file) for the object. Use this command line:

    jpub -props=RationalO.props
    
    

    where the properties file RationalO.props contains:

    jpub.user=scott/tiger
    jpub.sql=RationalO:JPubRationalO:RationalO
    jpub.methods=true
    
    

    According to the properties file, JPublisher will log into the database with user name scott and password tiger. The sql parameter directs JPublisher to translate the object type RationalO (declared by RatinalO.sql) and generate JPubRationalO as RationalO, where the second RationalO indicates a class that you have written (RationalO.java) that extends the functionality of the original RationalO. The value of the methods parameter indicates that JPublisher will generate classes for PL/SQL packages and wrapper methods.

    JPublisher produces the files:

    JPubRationalO.sqlj
    RationalORef.java
    
    

    See "Listing of JPubRationalO.sqlj Generated by JPublisher" and "Listing of RationalORef.java Generated by JPublisher" for listings of the JPubRationalO.sqlj and RationalORef.java files.

  3. Write a file RationalO.java that enhances the functionality of JPubRationalO.sqlj by extending it. In RationalO.java, everything is inherited from the superclass except the following items. You add code to:

    • declare a factory object, _JPubRationalO

    • implement a getFactory() method

    • implement a create() method

    • implement the constructors by calling the constructors in the superclass

    • add a toString() method, which is used in the last two System.out.println() calls in the test program TestRationalO.java (described in "Listing of TestRationalO.java Written by a User")

    "Listing of RationalO.java Written by a User" contains the code for the RationalO.java file.

  4. Compile/translate the necessary files. Enter:

    sqlj JPubRationalO.sqlj RationalO.java
    
    

    to translate JPubRationalO.sqlj and compile the RationalO.java file.

  5. Write a program TestRationalO.java that uses the RationalO.java class. "Listing of TestRationalO.java Written by a User" contains the code for TestRationalO.java.

  6. Write the file connect.properties, which TestRationalO.java uses to determine how to connect to the database. The file reads as follows:

    sqlj.user=scott
    sqlj.password=tiger
    sqlj.url=jdbc:oracle:oci8:@
    sqlj.driver=oracle.jdbc.driver.OracleDriver
    
    
  7. Compile, then run TestRationalO.java:

    javac TestRationalO.java
    java TestRationalO
    
    

    The program produces the following output:

    gcd: 5
    real value: 0.5
    sum: 100/100
    sum: 1/1
    

Listing of RationalO.sql to Create the Object Type

This section contains the code that defines the RationalO SQL object type.

CREATE TYPE RationalO AS OBJECT (
   numerator INTEGER,
   denominator INTEGER,
   MAP MEMBER FUNCTION toReal RETURN REAL,
   MEMBER PROCEDURE normalize,
   STATIC FUNCTION gcd(x INTEGER,
                       y INTEGER) RETURN INTEGER,
   MEMBER FUNCTION plus ( x RationalO) RETURN RationalO
);

CREATE TYPE BODY RationalO AS

  MAP MEMBER FUNCTION toReal RETURN REAL IS
  -- convert rational number to real number
  BEGIN
    RETURN numerator / denominator;
  END toReal;

  MEMBER PROCEDURE normalize IS
   g BINARY_INTEGER;
  BEGIN
   g := RationalO.gcd(numerator, denominator);
   numerator := numerator / g;
   denominator := denominator / g;
  END normalize;

  STATIC FUNCTION gcd(x INTEGER,
                      y INTEGER) RETURN INTEGER IS
  -- find greatest common divisor of x and y
  ans BINARY_INTEGER;
  BEGIN
  IF x < y THEN
     ans := RationalO.gcd(y, x);
  ELSIF (x MOD y = 0) THEN
     ans := y;
  ELSE
     ans := RationalO.gcd(y, x MOD y);
  END IF;
  RETURN ans;
  END gcd;

  MEMBER FUNCTION plus (x RationalO) RETURN RationalO IS
  BEGIN
   return RationalO(numerator * x.denominator + x.numerator * denominator,
                   denominator * x.denominator);
  END plus;
END;

Listing of JPubRationalO.sqlj Generated by JPublisher

This section lists the code for JPubRationalO.java that JPublisher generates.

import java.sql.SQLException;
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.driver.OracleTypes;
import oracle.sql.CustomDatum;
import oracle.sql.CustomDatumFactory;
import oracle.sql.Datum;
import oracle.sql.STRUCT;
import oracle.jpub.runtime.MutableStruct;
import sqlj.runtime.ref.DefaultContext;
import sqlj.runtime.ConnectionContext;
import java.sql.Connection;

public class JPubRationalO implements CustomDatum, CustomDatumFactory
{
  public static final String _SQL_NAME = "SCOTT.RATIONALO";
  public static final int _SQL_TYPECODE = OracleTypes.STRUCT;

  #sql static context _Ctx;
  _Ctx _ctx;

  MutableStruct _struct;

  static int[] _sqlType =
  {
    4, 4
  };

  static CustomDatumFactory[] _factory = new CustomDatumFactory[2];

  static final JPubRationalO _JPubRationalOFactory = new JPubRationalO();
  public static CustomDatumFactory getFactory()
  {
    return _JPubRationalOFactory;
  }

  /* constructors */
  public JPubRationalO()
  {
    _struct = new MutableStruct(new Object[2], _sqlType, _factory);
    try
    {
      _ctx = new _Ctx(DefaultContext.getDefaultContext());
    }
    catch (Exception e)
    {
      _ctx = null;
    }
  }

  public JPubRationalO(ConnectionContext c) throws SQLException
  {
    _struct = new MutableStruct(new Object[2], _sqlType, _factory);
    _ctx = new _Ctx(c == null ? DefaultContext.getDefaultContext()
                              : c);
  }
  public JPubRationalO(Connection c) throws SQLException
  {
    _struct = new MutableStruct(new Object[2], _sqlType, _factory);
    _ctx = new _Ctx(c);
  }

  /* CustomDatum interface */
  public Datum toDatum(OracleConnection c) throws SQLException
  {
    _ctx = new _Ctx(c);
    return _struct.toDatum(c, _SQL_NAME);
  }

  /* CustomDatumFactory interface */
  public CustomDatum create(Datum d, int sqlType) throws SQLException
  {
    if (d == null) return null;
    JPubRationalO o = new JPubRationalO();
    o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
    o._ctx = new _Ctx(((STRUCT) d).getConnection());
    return o;
  }

  /* accessor methods */
  public Integer getNumerator() throws SQLException
  { return (Integer) _struct.getAttribute(0); }

  public void setNumerator(Integer numerator) throws SQLException
  { _struct.setAttribute(0, numerator); }


  public Integer getDenominator() throws SQLException
  { return (Integer) _struct.getAttribute(1); }

  public void setDenominator(Integer denominator) throws SQLException
  { _struct.setAttribute(1, denominator); }


  public Integer gcd (
    Integer x,
    Integer y)
  throws SQLException
  {
    Integer __jPt_result;
    #sql [_ctx] __jPt_result = { VALUES(RATIONALO.GCD(
      :x,
      :y)) };
    return __jPt_result;
  }

  public RationalO normalize ()
  throws SQLException
  {
    RationalO __jPt_temp = (RationalO) this;
    #sql [_ctx] {
      BEGIN
      :INOUT __jPt_temp.NORMALIZE();
      END;
    };
    return __jPt_temp;
  }

  public RationalO plus (
    RationalO x)
  throws SQLException
  {
    JPubRationalO __jPt_temp = this;
    RationalO __jPt_result;
    #sql [_ctx] {
      BEGIN
      :OUT __jPt_result := :__jPt_temp.PLUS(
      :x);
      END;
    };
    return __jPt_result;
  }

  public Float toreal ()
  throws SQLException
  {
    JPubRationalO __jPt_temp = this;
    Float __jPt_result;
    #sql [_ctx] {
      BEGIN
      :OUT __jPt_result := :__jPt_temp.TOREAL();
      END;
    };
    return __jPt_result;
  }
}

Listing of RationalORef.java Generated by JPublisher

This section lists the code for RationalORef.java that JPublisher generates.


Note:

The details of method bodies that JPublisher generates might change in future releases.  


import java.sql.SQLException;
import oracle.jdbc.driver.OracleConnection;
import oracle.jdbc.driver.OracleTypes;
import oracle.sql.CustomDatum;
import oracle.sql.CustomDatumFactory;
import oracle.sql.Datum;
import oracle.sql.REF;
import oracle.sql.STRUCT;

public class RationalORef implements CustomDatum, CustomDatumFactory
{
  public static final String _SQL_BASETYPE = "SCOTT.RATIONALO";
  public static final int _SQL_TYPECODE = OracleTypes.REF;

  REF _ref;

  static final RationalORef _RationalORefFactory = new RationalORef();
  public static CustomDatumFactory getFactory()
  {
    return _RationalORefFactory;
  }

  /* constructor */
  public RationalORef()
  {
  }

  /* CustomDatum interface */
  public Datum toDatum(OracleConnection c) throws SQLException
  {
    return _ref;
  }

  /* CustomDatumFactory interface */
  public CustomDatum create(Datum d, int sqlType) throws SQLException
  {
    if (d == null) return null;
    RationalORef r = new RationalORef();
    r._ref = (REF) d;
    return r;
  }
  public RationalO getValue() throws SQLException
  {
     return (RationalO) RationalO.getFactory().create(
       _ref.getSTRUCT(), OracleTypes.REF);
  }

  public void setValue(RationalO c) throws SQLException
  {
    _ref.setValue((STRUCT) c.toDatum(_ref.getConnection()));
  }
}

Listing of RationalO.java Written by a User

This section lists the code for the user-written file, RationalO.java, that extends the class JPubRationalO.sqlj. Note that this program:

Listing of TestRationalO.java Written by a User

This section lists the contents of a user-written file, TestRationalO.java, that tests the performance of the RationalO.java class, given initial values for numerator and denominator. Note that the TestRationalO.java file also demonstrates how to:



Go to previous page
Go to beginning of chapter
Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index