Oracle8i JDBC Developer's Guide and Reference Release 3 (8.1.7) Part Number A83724-01 |
Library | Service | Contents | Index

## Describing an Object Type

Release 8.1.6 and higher includes new functionality to retrieve information about a structured object type regarding its attribute names and types. This is similar conceptually to retrieving information from a result set about its column names and types, and in fact uses an almost identical API.

### Functionality for Getting Object Meta Data

The `oracle.sql.StructDescriptor` class, discussed earlier in "STRUCT Descriptors" and "Steps in Creating StructDescriptor and STRUCT Objects", now includes functionality to retrieve meta data about a structured object type.

The `StructDescriptor` class has a `getMetaData()` method with the same functionality as the standard `getMetaData()` method available in result set objects. It returns a set of attribute information such as attribute names and types. Call this method on a `StructDescriptor` object to get meta data about the Oracle object type that the `StructDescriptor` object describes. (Remember that each structured object type must have an associated `StructDescriptor` object.)

The signature of the `StructDescriptor` class `getMetaData()` method is the same as the signature specified for `getMetaData()` in the standard `ResultSet` interface:

- `ResultSetMetaData getMetaData() throws SQLException`

However, this method actually returns an instance of `oracle.jdbc.driver.StructMetaData`, a class that supports structured object meta data in the same way that the standard `java.sql.ResultSetMetaData` interface specifies support for result set meta data.

The `StuctMetaData` class includes the following standard methods that are also specified by `ResultSetMetaData`:

- `String getColumnName(int column) throws SQLException`

  This returns a `String` that specifies the name of the specified attribute, such as "salary".

- `int getColumnType(int column) throws SQLException`

  This returns an `int` that specifies the typecode of the specified attribute, according to the `java.sql.Types` and `oracle.jdbc.driver.OracleTypes` classes.

- `String getColumnTypeName(int column) throws SQLException`

  This returns a string that specifies the type of the specified attribute, such as "BigDecimal".

- `int getColumnCount() throws SQLException`

  This returns the number of attributes in the object type.

As well as the following method, supported only by `StructMetaData`:

- `String getOracleColumnClassName(int column)
  throws SQLException`

  This returns the fully-qualified name of the `oracle.sql.Datum` subclass whose instances are manufactured if the `OracleResultSet` class `getOracleObject()` method is called to retrieve the value of the specified attribute. For example, "oracle.sql.NUMBER".

  To use `getOracleColumnClassName()`, you must cast the `ResultSetMetaData` object (that was returned by the `getMetaData()` method) to a `StructMetaData` object.

  Note: In all the preceding method signatures, "column" is something of a misnomer. Where you specify a "column" of 4, you really refer to the fourth attribute of the object.

### Steps for Retrieving Object Meta Data

Use the following steps to obtain meta data about a structured object type:

1. Create or acquire a `StructDescriptor` instance that describes the relevant structured object type.

2. Call the `getMetaData()` method on the `StructDescriptor` instance.

3. Call the meta data getter methods as desired--`getColumnName()`, `getColumnType()`, and `getColumnTypeName()`.

   Note: If one of the structured object attributes is itself a structured object, repeat steps 1 through 3.

##### Example

The following method shows how to retrieve information about the attributes of a structured object type. This includes the initial step of creating a `StructDescriptor` instance.

    //
    // Print out the ADT's attribute names and types
    //
    void getAttributeInfo (Connection conn, String type_name) throws SQLException
    {
      // get the type descriptor
      StructDescriptor desc = StructDescriptor.createDescriptor (type_name, conn);

      // get type meta data
      ResultSetMetaData md = desc.getMetaData ();

      // get # of attrs of this type
      int numAttrs = desc.length ();

      // temporary buffers
      String attr_name;
      int attr_type;
      String attr_typeName;

      System.out.println ("Attributes of "+type_name+" :");
      for (int i=0; i<numAttrs; i++)
      {
        attr_name = md.getColumnName (i+1);
        attr_type = md.getColumnType (i+1);
        System.out.println (" index"+(i+1)+" name="+attr_name+" type="+attr_type);

        // drill down nested object
        if (attrType == OracleTypes.STRUCT)
        {
          attr_typeName = md.getColumnTypeName (i+1);

          // recursive calls to print out nested object meta data
          getAttributeInfo (conn, attr_typeName);
        }
      }
    }

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index