![]() |
The Java Developers Almanac 1.4 |
|
e299. Getting an OBJECT Value from an Oracle TableThis example retrieves values contained in an Oracle OBJECT type. The example uses the table and types created in e296 Creating an OBJECT Type in an Oracle Database. try {
// Create a statement
Statement stmt = connection.createStatement();
// Select rows from object1_table
ResultSet resultSet = stmt.executeQuery("SELECT * FROM object1_table");
// Get the OBJECT values from each row
while (resultSet.next()) {
// Get the integer from the first column col_integer of the row
int i = resultSet.getInt(1);
// Get the object1 value from the second column col_object1
oracle.sql.STRUCT object1 = (oracle.sql.STRUCT)resultSet.getObject(2);
// Get the object1 values from each row
Object[] object1Values = object1.getAttributes();
// Get the first value of object1, which is a string
String str = (String)object1Values[0];
// Get the second value of object1, which is of the type object2
oracle.sql.STRUCT object2 = (oracle.sql.STRUCT)object1Values[1];
// Get the values of object2
Object object2Values[] = object2.getAttributes();
str = (String)object2Values[0];
BigDecimal num = (BigDecimal)object2Values[1];
}
} catch (SQLException e) {
}
e297. Inserting an OBJECT Value into an Oracle Table e298. Inserting an OBJECT Value into an Oracle Table Using a Prepared Statement e300. Deleting an OBJECT Type from an Oracle Table © 2002 Addison-Wesley. |