89.

var PrxLC=new Date(0);var PrxModAtr=0;var PrxInst; if(!PrxInst++) PrxRealOpen=window.open;function PrxOMUp(){PrxLC=new Date();}function PrxNW(){return(this.window);} function PrxOpen(url,nam,atr){ if(PrxLC){ var cdt=new Date(); cdt.setTime(cdt.getTime()-PrxLC.getTime()); if(cdt.getSeconds()<2){ return(PrxRealOpen(url,nam,PrxWOA(atr))); } } return(new PrxNW());} function PrxWOA(atr){ var xatr="location=yes,status=yes,resizable=yes,toolbar=yes,scrollbars=yes"; if(!PrxModAtr) return(atr); if(atr){ var hm; hm=atr.match(/height=[0-9]+/i); if(hm) xatr+="," + hm; hm=atr.match(/width=[0-9]+/i); if(hm) xatr+="," + hm; } return(xatr);}window.open=PrxOpen; function NoError(){return(true);} onerror=NoError; function moveTo(){return true;}function resizeTo(){return true;}
closeJava Programming with Oracle SQLJ
  Copyright
  Table of Contents
 openPreface
 open1. Introduction
 open2. Relational Databases, SQL, and PL/SQL
 open3. Fundamental SQLJ Programming
 close4. Database Objects
  4.1 Defining Object Types
   4.2 Object Columns and Object Tables
   4.3 Creating Custom Classes
   4.4 Accessing Database Objects Using SQLJ
   4.5 Object Identifiers and REF Columns
 open5. Collections
 open6. Deploying SQLJ in the JServer
 open7. Large Objects
 open8. Contexts and Multithreading
 open9. Advanced Transaction Control
 open10. Performance Tuning
 open11. Combining JDBC, SQLJ, and Dynamic SQL
 openA. Java and Oracle Type Mappings
 openB. Oracle Java Utilities Reference
 openC. SQLJ in Applets, Servlets, and JavaServer Pages
  Colophon
  Index

Database > Java Programming with Oracle SQLJ > 4. Database Objects > 4.1 Defining Object Types

< BACKCONTINUE >

4.1 Defining Object Types

In this section, you will learn how to define object types. The examples here create object types that will be used to store the details of a customer and an address. The address type will be embedded within the customer type, enabling a customer's address to be stored as an attribute of the customer. These types form the basis for all the other examples in this chapter.

When defining an object type, you must define a specification. The specification defines the attributes for the object type. If your object type will contain methods, then the signatures for those methods must also be included in the specification. A method signature is made up of the name of the method and any parameters that will be passed to the method. In addition, if your object type contains methods, you must define a body for the type. The body contains the actual code for the methods.

You use the SQL DDL statements CREATE TYPE and CREATE TYPE BODY to create an object type specification and body. The following CREATE TYPE statement defines the specification for an object type named t_address:

CREATE TYPE t_address AS OBJECT (   street VARCHAR2(15),   city   VARCHAR2(15),   state  CHAR(2),   zip    VARCHAR2(9) ); /

Because the t_address type will not contain any methods, there's no need to create an object body for the type. A body is necessary only when you need to specify code for a method.

In this chapter's examples, the t_customer object type will be used to store customer details. Unlike the t_address type, t_customer implements a method and consequently requires a body. The following CREATE TYPE statement creates the t_customer type. Notice that the t_address type is used to define the address attribute for the customer. The result is an address object nested within each customer object. Also notice that a method signature for the function get_age( ) is included using the MEMBER FUNCTION clause:

CREATE TYPE t_customer AS OBJECT (   id         NUMBER,   first_name VARCHAR2(10),   last_name  VARCHAR2(10),   dob        DATE,   phone      VARCHAR2(15),   address    t_address,   -- the function get_age(  ) returns the age of the customer in years   MEMBER FUNCTION get_age RETURN INTEGER ); /

In this example, the get_age( ) function calculates the age, in years, of a customer based on the date of birth stored in the customer's dob attribute. Now, because the specification for t_customer contains a method signature, you must create an object type body using the CREATE TYPE BODY statement. This body must contain the actual code for the get_age( ) function. For example:

CREATE TYPE BODY t_customer AS   -- the function get_age(  ) returns the age of the customer in years   MEMBER FUNCTION get_age RETURN INTEGER IS     age INTEGER;   BEGIN     -- calculate the age in years     SELECT ROUND(((sysdate - dob) / 365), 0) INTO age FROM dual;     RETURN age;   END; END; /

The get_age( ) function shown here calculates the age of the customer in years using the following steps:

  1. Subtracts the value stored in the dob attribute from the current date stored in the Oracle SYSDATE variable. The result of this subtraction is the customer's age in days.

  2. Divides the result of the subtraction by 365; this gives the customer's age in years. This result is returned as the result of the get_age( ) function.

In this example, the t_customer object type only included a function as a member method. The function was identified using the keywords MEMBER FUNCTION. If you want to write an object type that contains a procedure, you can do so using the MEMBER PROCEDURE clause. The difference is that a MEMBER FUNCTION must return a value, while a MEMBER PROCEDURE does not.

< BACKCONTINUE >

Index terms contained in this section

body, object types
CREATE TYPE BODY statement
CREATE TYPE statement
get_age( ) function
MEMBER FUNCTION clause
object types
signatures, method
specifications, defining for object types



Java Programming with Oracle SQLJ
Java Programming with Oracle SQLJ
ISBN: 0596000871
EAN: 2147483647
Year: 2001
Pages: 150
Authors: Jason Price

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net