Getting a STRUCT Using OracleStruct Extension Methods Getting STRUCT Attributes

Using API Extensions in JDBC Drivers 5-11 4. Cast the STRUCT as a STRUCT, either java.sql.Struct to use standard methods or weblogic.jdbc.vendor.oracle.OracleStruct to use standard and Oracle extension methods. 5. Use the standard or Oracle Thin driver extension methods to work with the data. The following sections provide more details for steps 3 through 5.

5.2.4.1 Getting a STRUCT

To get a database object as a STRUCT, you can use a query to create a result set and then use the getObject method to get the STRUCT from the result set. You then cast the STRUCT as a java.sql.Struct so you can use the standard Java methods. For example: conn = ds.getConnection; stmt = conn.createStatement; rs = stmt.executeQueryselect from people; struct = java.sql.Structrs.getObject2; Object[] attrs = java.sql.Structstruct.getAttributes; WebLogic Server supports all of the JDBC API methods for STRUCTs: ■ getAttributes ■ getAttributesjava.util.Dictionary map ■ getSQLTypeName When you cast a STRUCT as a weblogic.jdbc.vendor.oracle.OracleStruct, you can use both the standard and extension methods.

5.2.4.2 Using OracleStruct Extension Methods

To use the Oracle Thin driver extension methods for a STRUCT, you must cast the java.sql.Struct or the original getObject result as a weblogic.jdbc.vendor.oracle.OracleStruct . For example: java.sql.Struct struct = weblogic.jdbc.vendor.oracle.OracleStructrs.getObject2; WebLogic Server supports the following Oracle extensions: ■ getDescriptor ■ getOracleAttributes ■ getAutoBuffering ■ setAutoBufferingboolean

5.2.4.3 Getting STRUCT Attributes

To get the value for an individual attribute in a STRUCT, you can use the standard JDBC API methods getAttributes and getAttributesjava.util.Dictionary map , or you can use the Oracle extension method getOracleAttributes. To use the standard method, you can create a result set, get a STRUCT from the result set, and then use the getAttributes method. The method returns an array of ordered attributes. You can assign the attributes from the STRUCT object in the database to an object in the application, including Java language types. You can then manipulate the attributes individually. For example: 5-12 Oracle Fusion Middleware Programming JDBC for Oracle WebLogic Server conn = ds.getConnection; stmt = conn.createStatement; rs = stmt.executeQueryselect from people; The third column uses an object data type. Use getObject to assign the object to an array of values. struct = java.sql.Structrs.getObject2; Object[] attrs = java.sql.Structstruct.getAttributes; String address = attrs[1]; In the preceding example, the third column in the people table uses an object data type. The example shows how to assign the results from the getObject method to a Java object that contains an array of values, and then use individual values in the array as necessary. You can also use the getAttributesjava.util.Dictionary map method to get the attributes from a STRUCT. When you use this method, you must provide a hash table to map the data types in the Oracle object to Java language data types. For example: java.util.Hashtable map = new java.util.Hashtable; map.putNUMBER, Class.forNamejava.lang.Integer; map.putVARCHAR, Class.forNamejava.lang.String; Object[] attrs = java.sql.Structstruct.getAttributesmap; String address = attrs[1]; You can also use the Oracle extension method getOracleAttributes to get the attributes for a STRUCT. You must first cast the STRUCT as a weblogic.jdbc.vendor.oracle.OracleStruct . This method returns a datum array of oracle.sql.Datum objects. For example: oracle.sql.Datum[] attrs = weblogic.jdbc.vendor.oracle.OracleStructstruct.getOracleAttributes; oracle.sql.STRUCT address = oracle.sql.STRUCT attrs[1]; Object address_attrs[] = address.getAttributes; The preceding example includes a nested STRUCT. That is, the second attribute in the datum array returned is another STRUCT.

5.2.4.4 Using STRUCTs to Update Objects in the Database