96.

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
 open4. Database Objects
 close5. Collections
  5.1 VARRAYs
   5.2 Nested Tables
   5.3 Storing an Object Type in a Collection
   5.4 Collection Custom Classes
   5.5 Accessing Collections Using SQLJ
 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 > 5. Collections > 5.1 VARRAYs

< BACKCONTINUE >

5.1 VARRAYs

A VARRAY is an ordered set of elements. The maximum number of elements in a VARRAY is set when the VARRAY is created. You create a VARRAY by first declaring a type, and then using that type to define a column in a table. The following example creates a VARRAY type, named t_address2, that contains three VARCHAR2 strings:

CREATE TYPE t_address2 AS VARRAY(3) OF VARCHAR2(50); /

Each VARCHAR2 string in this type will be used to represent an address for a customer. Once you have created a VARRAY type, you can define a column that uses it. The following example creates a table named customers3, which contains a column named addresses that is based on the t_address2 type:

CREATE TABLE customers3 (   id         NUMBER,   first_name VARCHAR2(10),   last_name  VARCHAR2(10),   addresses  t_address2 );

In order to add a row containing a VARRAY to this table, you must place the elements for the VARRAY in a constructor for the VARRAY type. The following example adds a row to the customers3 table, placing two elements for the VARRAY in the constructor for the t_address2 type:

INSERT INTO customers3 VALUES (   1, 'John', 'Smith',   t_address2(     '1 Anystreet, Anytown, CA, 12345',     '2 Main Street, Big Town, NY, 54321'   ) );

After this INSERT is performed, the customers3 table contains one row. The VARRAY stored in the addresses column contains two strings, one for each of the strings contained in the t_address2 constructor.

From the previous examples, you can see that the use of VARRAYs is similar to the use of database objects, which were described in Chapter 4. With VARRAYs, as with database objects, you must create a type, and just as you use a constructor to populate an object, you must use a constructor to populate a VARRAY.

5.1.1 Retrieving the Contents of a VARRAY

Now that you have seen how to create and populate a VARRAY, you may wonder how to retrieve a VARRAY using a SELECT statement. The answer is that you don't have to do anything special. You simply select the VARRAY column as you would any other. For example:

SQL> SELECT *   2  FROM customers3   3  WHERE id = 1;  ID FIRST_NAME LAST_NAME --- ---------- --------- ADDRESSES ---------------------------------------------   1 John       Smith T_ADDRESS2('1 Anystreet, Anytown, CA, 12345', '2 Main Street, Big Town, NY, 54321')

Notice in this SQL*Plus output that the values for the addresses column appear in a constructor for the t_address2 type. Both the strings that were inserted into the VARRAY are contained within this constructor. This is similar to the way in which SQL*Plus displays database objects that you retrieve from object columns.

5.1.2 Modifying the Contents of a VARRAY

When you modify a VARRAY, you must rebuild the entire array: you can't go in and change just one element independently of the others. You have to retrieve the VARRAY, modify the elements you want to change, and then recreate the entire VARRAY, including the elements that haven't changed, in an UPDATE statement. The following UPDATE statement modifies the first string in the VARRAY that was created in the previous example:

UPDATE   customers3 SET   addresses =  t_address2(     '1 New Street, New Town, CA, 12345',     '2 Main Street, Big Town, NY, 54321'   ) WHERE   id = 1;

Note that even though the second string in the VARRAY hasn't changed, it must still be included in the UPDATE statement. In effect, you are replacing the old VARRAY in the addresses column with a completely new VARRAY created in your UPDATE statement.

< BACKCONTINUE >

Index terms contained in this section

collections
      VARRAY
CREATE TYPE statement
      VARRAY
INSERT statement, SQL
      VARRAY
SELECT statement
      VARRAY, retrieving
UPDATE statement, SQL
      VARRAY
VARRAY
      CREATE TABLE statement
      INSERT statement
      SELECT statement
      UPDATE statement, SQL



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