Collection examples
create table tab ( a set(integer not null), b integer);
insert into tab values ("set{1, 2, 3}", 10);
java.util.HashSet set;
PreparedStatement pstmt;
ResultSet rs;
pstmt = conn.prepareStatement("select * from tab");
System.out.println("prepare ... ok");
rs = pstmt.executeQuery();
System.out.println("executeQuery ... ok");
rs.next();
set = (HashSet) rs.getObject(1);
System.out.println("getObject() ... ok");
/* The user can now use HashSet.iterator() to extract
* each element in the collection.
*/
Iterator it = set.iterator();
Object obj;
Class cls = null;
int i = 0;
while (it.hasNext())
{
obj = it.next();
if (cls == null)
{
cls = obj.getClass();
System.out.println(" Collection class: " + cls.getName());
}
System.out.println(" element[" + i + "] = " +
obj.toString());
i++;
}
pstmt.close();
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.
java.util.TreeSet set;
PreparedStatement pstmt;
ResultSet rs;
/*
* Fetch a SET as a TreeSet instead of the default
* HashSet. In this example a new java.util.Map object has
* been allocated and passed in as a parameter to getObject().
* Connection.getTypeMap() could have been used as well.
*/
java.util.Map map = new HashMap();
map.put("set", Class.forName("java.util.TreeSet"));
System.out.println("mapping ... ok");
pstmt = conn.prepareStatement("select * from tab");
System.out.println("prepare ... ok");
rs = pstmt.executeQuery();
System.out.println("executeQuery ... ok");
rs.next();
set = (TreeSet) rs.getObject(1, map);
System.out.println("getObject(Map) ... ok");
/* The user can now use HashSet.iterator() to extract
* each element in the collection.
*/
Iterator it = set.iterator();
Object obj;
Class cls = null;
int i = 0;
while (it.hasNext())
{
obj = it.next();
if (cls == null)
{
cls = obj.getClass();
System.out.println(" Collection class: " + cls.getName());
}
System.out.println(" element[" + i + "] = " +
obj.toString());
i++;
}
pstmt.close();
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.
java.util.HashSet set = new HashSet();
Integer intObject;
int i;
/* Populate the Java collection */
for (i=0; i < 5; i++)
{
intObject = new Integer(i);
set.add(intObject);
}
System.out.println("populate java.util.HashSet...ok");
PreparedStatement pstmt = conn.prepareStatement
("insert into tab values (?, 20)");
System.out.println("prepare...ok");
pstmt.setObject(1, set);
System.out.println("setObject()...ok");
pstmt.executeUpdate();
System.out.println("executeUpdate()...ok");
pstmt.close();
The pstmt.setObject(1, set)
statement
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.