Array example
Following is a sample database schema:
CREATE TABLE tab (a set(integer not null), b integer); INSERT INTO tab VALUES ("set{1,2,3}", 10);
The following example fetches data using a java.sql.Array object:
PreparedStatement pstmt = conn.prepareStatement("select a from tab"); System.out.println("prepare ... ok"); ResultSet rs = pstmt.executeQuery(); System.out.println("executeQuery ... ok"); rs.next(); java.sql.Array array = rs.getArray(1); System.out.println("getArray() ... ok"); pstmt.close(); /* * The user can now materialize the data into either * an array or else a ResultSet. If the collection elements * are primitives then the array should be an array of primitives, * not Objects. Mapping data can be provided at this point. */ Object obj = array.getArray((long) 1, 2); int [] intArray = (int []) obj; // cast it to an array of ints int i; for (i=0; i < intArray.length; i++) { System.out.println("integer element = " + intArray[i]); } pstmt.close();
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 (int types,
not Integer objects). Because the getArray() method
has been called with index and count values, only a subset of data
is returned.