Retrieving Array Objects
Data of type SQL ARRAY can be retrieved by calling the getArray method of the
ResultSet and CallableStatement interfaces. For example, the following line
of code retrieves an Array value from the first column of the ResultSet rs.
Array a = rs.getArray(1);
By default, a JDBC driver should implement the Array interface using an SQL
LOCATOR(array) internally. Also by default, Array objects remain valid only
during the transaction in which they are created.
The Array object returned to an application by the ResultSet.getArray and
CallableStatement.getArray methods is a logical pointer to the SQL ARRAY
value in the database; it does not contain the contents of the SQL ARRAY value. The
Array interface provides several versions of the methods getArray and
getResultSet that return the contents of an SQL ARRAY value to the client as a
materialized Java programming language array (Java array) or as a ResultSet
object. The API documentation gives complete details.
Storing Array Objects
The PreparedStatement methods setArray and setObject may be called to
pass an Array value as an input parameter to a PreparedStatement object.
CODE EXAMPLE :sets the Array object member_array, which was retrieved from
another table in the database, as the second parameter to the PreparedStatement
pstmt.
PreparedStatement pstmt = conn.prepareStatement(
"INSERT INTO dept (name, members) VALUES (?, ?)");
pstmt.setString(1, "biology");
pstmt.setArray(2, member_array);
pstmt.executeUpdate();
CODE EXAMPLE : Storing an Array object. A Java array may be passed as an input parameter by calling the method
PreparedSatement.setObject.
Updating Array Objects
The ResultSet methods updateArray and updateObject can be used to update
a column value.
CODE EXAMPLE: uses the method ResultSet.updateArray to update the value
of the column LATEST_NUMBERS in one ResultSet object with an Array object
retrieved from the column NUMBERS in another ResultSet object.
// retrieve a column containing an SQL ARRAY value from ResultSet rs
java.sql.Array num = rs.getArray("NUMBERS");
...
// update the column "LATEST_NUMBERS" in a second ResultSet
// with the value retrieved...
rs2.updateArray("LATEST_NUMBERS", num);
rs2.updateRow();
Saturday, November 10, 2007
Array Objects:JAVA
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment