Import Packages to Access Oracle Extensions Establish the Connection Getting an ARRAY Updating ARRAYs in the Database

Using API Extensions in JDBC Drivers 5-9

5.2.3.1 Import Packages to Access Oracle Extensions

Import the WebLogic interfaces used in this example. The OracleArray interface is counterpart to oracle.sql.ARRAY and can be used in the same way as the WebLogic interface when using the methods supported by WebLogic Server. import java.sql.; import java.util.; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import weblogic.jdbc.vendor.oracle.;

5.2.3.2 Establish the Connection

Establish the database connection using JNDI and DataSource objects. Get a valid DataSource object. Here we assume that getDataSource takes care of those details. javax.sql.DataSource ds = getDataSourceargs; get a java.sql.Connection object from the DataSource java.sql.Connection conn = ds.getConnection;

5.2.3.3 Getting an ARRAY

You can use the getArray methods for a callable statement or a result set to get a Java array. You can then use the array as a java.sql.array to use standard java.sql.array methods, or you can cast the array as a weblogic.jdbc.vendor.oracle.OracleArray to use the Oracle extension methods for an array. The following example shows how to get a java.sql.array from a result set that contains an ARRAY. In the example, the query returns a result set that contains an object column—an ARRAY of test scores for a student. try { conn = getConnectionurl; stmt = conn.createStatement; String sql = select from students; Get the result set rs = stmt.executeQuerysql; whilers.next { BigDecimal id = rs.getBigDecimalstudent_id; String name = rs.getStringname; logArraysDAO.getStudents -- Id = +id.toString+, Student = +name; Get the array from the result set Array scoreArray = rs.getArraytest_scores; String[] scores = String[]scoreArray.getArray; for int i = 0; i scores.length; i++ { log Test+i+1+ = +scores[i]; } }

5.2.3.4 Updating ARRAYs in the Database

To update an ARRAY in a database, you can Follow these steps: Note: You can use ARRAYs in server-side applications only. You cannot use ARRAYs in remote client applications. 5-10 Oracle Fusion Middleware Programming JDBC for Oracle WebLogic Server 1. Create an array in the database using PLSQL, if the array you want to update does not already exist in the database. 2. Get the ARRAY using a result set or a callable statement. 3. Work with the array in your Java application as either a java.sql.Array or a weblogic.jdbc.vendor.oracle.OracleArray . 4. Update the array in the database using the setArray method for a prepared statement or a callable statement. For example: String sqlUpdate = UPDATE SCOTT. + tableName + SET col1 = ?; conn = ds.getConnection; pstmt = conn.prepareStatementsqlUpdate; pstmt.setArray1, array; pstmt.executeUpdate;

5.2.3.5 Using Oracle Array Extension Methods