Updating REF Values Programming with REFs

Using API Extensions in JDBC Drivers 5-15 The example below shows how to use the getValue method without parameters. In this example, the REF is cast as an oracle.sql.STRUCT. You can then use the STRUCT methods to manipulate the value, as illustrated with the getAttributes method. oracle.sql.STRUCT student1 = oracle.sql.STRUCTweblogic.jdbc.vendor.oracle.OracleRefref.getValue ; Object attributes[] = student1.getAttributes; You can also use the getValuedictionary method to get the value for a REF. You must provide a hash table to map data types in each attribute of the REF to Java language data types. For example: java.util.Hashtable map = new java.util.Hashtable; map.putVARCHAR, Class.forNamejava.lang.String; map.putNUMBER, Class.forNamejava.lang.Integer; oracle.sql.STRUCT result = oracle.sql.STRUCT weblogic.jdbc.vendor.oracle.OracleRefref.getValue map;

5.2.5.4 Updating REF Values

When you update a REF, you can do any of the following: ■ Change the value in the underlying table with the setValueobject method. ■ Change the location to which the REF points with a prepared statement or a callable statement. ■ Set the value of the REF to null. To use the setValueobject method to update a REF value, you create an object with the new values for the REF, and then pass the object as a parameter of the setValue method. For example: STUDENT s1 = new STUDENT; s1.setNameTerry Green; s1.setAge20; weblogic.jdbc.vendor.oracle.OracleRefref.setValues1; When you update the value for a REF with the setValueobject method, you actually update the value in the table to which the REF points. To update the location to which a REF points using a prepared statement, you can follow these basic steps: 1. Get a REF that points to the new location. You use this REF to replace the value of another REF. 2. Create a string for the SQL command to replace the location of an existing REF with the value of the new REF. 3. Create and execute a prepared statement. For example: try { conn = ds.getConnection; stmt = conn.createStatement; Get the REF. rs = stmt.executeQuerySELECT ref s FROM t1 s where s.ob1=5; rs.next; ref = java.sql.Ref rs.getRef1; cast the REF as a java.sql.Ref } Create and execute the prepared statement. 5-16 Oracle Fusion Middleware Programming JDBC for Oracle WebLogic Server String sqlUpdate = update t3 s2 set col = ? where s2.col.ob1=20; pstmt = conn.prepareStatementsqlUpdate; pstmt.setRef1, ref; pstmt.executeUpdate; To use a callable statement to update the location to which a REF points, you prepare the stored procedure, set any IN parameters and register any OUT parameters, and then execute the statement. The stored procedure updates the REF value, which is actually a location. For example: conn = ds.getConnection; stmt = conn.createStatement; rs = stmt.executeQuerySELECT ref s FROM t1 s where s.ob1=5; rs.next; ref1 = java.sql.Ref rs.getRef1; Prepare the stored procedure sql = {call SP1 ?, ?}; cstmt = conn.prepareCallsql; Set IN and register OUT params cstmt.setRef1, ref1; cstmt.registerOutParameter2, getRefType, USER.OB; Execute cstmt.execute;

5.2.5.5 Creating a REF in the Database