|
Sun's JDBC 2.0 specification describes only one method to exchange collection data between a Java client and a relational database: an array.
Because the array interface does not include a constructor, Informix JDBC Driver includes an extension that allows a java.util.Collection object to be used in the PreparedStatement.setObject() and ResultSet.getObject() methods. If you prefer to use an Array object, use the PreparedStatement.setArray() and ResultSet.getArray() methods.
By default, the driver maps LIST columns to java.util.ArrayList objects and SET and MULTISET columns to java.util.HashSet objects during a fetch. You can override these defaults, but the class you use must implement the java.util.Collection interface.
To override this default mapping, you can use other classes in the java.util.Collection interface, such as the TreeSet class. You can also create your own classes that implement the java.util.Collection interface. In either case, you must provide a customized type map using the Connection.setTypeMap() method.
During an INSERT operation, any java.util.Collection object that is an instance of the java.util.Set interface is mapped to an Informix MULTISET data type. An instance of the java.util.List interface is mapped to an Informix LIST data type. You can override these defaults by creating a customized type mapping.
For information about customized type mappings, see Mapping Data Types.
Important: Sets are by definition unordered. If you select collection data using a HashSet object, the order of the elements in the HashSet object might not be the same as the order specified when the set was inserted. For example, if the data on the database server is the set {1, 2, 3}, it might be retrieved into the HashSet object as {3, 2, 1} or any other order.
Collection Examples
Here is the database schema:
Here is a fetch example using a java.util.HashSet object. The complete demonstration is in the demo5.java file in the complex-types directory. For more information, see Appendix A, Sample Code Files.
In the set = (HashSet) rs.getObject(1) statement of this example, Informix JDBC Driver gets the type for column 1. Because it is a SET type, a HashSet object is instantiated. Next, each collection element is converted into a Java object and inserted into the collection.
Here is a fetch example using a java.util.TreeSet object. The complete demonstration is in the demo6.java file in the complex-types directory. For more information, see Appendix A, Sample Code Files.
In the map.put("set", Class.forName( "java.util.TreeSet" )); statement, the default mapping of set = HashSet is overridden.
In the set = (TreeSet) rs.getObject(1, map) statement, Informix JDBC Driver gets the type for column 1 and finds that it is a SET object. Then the driver looks up the type mapping information, finds TreeSet, and instantiates a TreeSet object. Next, each collection element is converted into a Java object and inserted into the collection.
Here is an example of an insert. The complete demonstration is in the demo7.java file in the complex-types directory. For more information, see Appendix A, Sample Code Files.
The pstmt.setObject(1, set) method in this example first serializes each element of the collection. Next, the type information is constructed as each element is converted into a Java object. If the types of any elements in the collection do not match the type of the first element, an exception is thrown. The type information is sent to the database server.
Here is the database schema:
Here is a fetch example using a java.sql.Array object. The complete demonstration is in the demo8.java file in the complex-types directory. For more information, see Appendix A, Sample Code Files.
The java.sql.Array array = rs.getArray(1) statement instantiates a java.sql.Array object. Data is not converted at this point.
The Object obj = array.getArray((long) 1, 2); statement converts data into an array of integers (primitive int types, not Integer objects). Because the getArray() method has been called with index and count values, only a subset of data is returned.