- Interface Array
- Method Summary
- Method Details
- getBaseTypeName
- getBaseType
- getArray
- getArray
- getArray
- getArray
- getResultSet
- getResultSet
- getResultSet
- getResultSet
- free
- Using Array Objects
- Creating Array Objects
- Retrieving and Accessing Array Values in ResultSet
- Storing and Updating Array Objects
- Releasing Array Resources
Interface Array
The mapping in the Java programming language for the SQL type ARRAY . By default, an Array value is a transaction-duration reference to an SQL ARRAY value. By default, an Array object is implemented using an SQL LOCATOR(array) internally, which means that an Array object contains a logical pointer to the data in the SQL ARRAY value rather than containing the ARRAY value’s data.
- create a class that implements the SQLData interface for the UDT to be custom mapped.
- make an entry in a type map that contains
- the fully-qualified SQL type name of the UDT
- the Class object for the class implementing SQLData
When a type map with an entry for the base type is supplied to the methods getArray and getResultSet , the mapping it contains will be used to map the elements of the ARRAY value. If no type map is supplied, which would typically be the case, the connection’s type map is used by default. If the connection’s type map or a type map supplied to a method has no entry for the base type, the elements are mapped according to the standard mapping.
All methods on the Array interface must be fully implemented if the JDBC driver supports the data type.
Method Summary
Retrieves the contents of the SQL ARRAY value designated by this Array object in the form of an array in the Java programming language.
Retrieves a slice of the SQL ARRAY value designated by this Array object, beginning with the specified index and containing up to count successive elements of the SQL array.
Retrieves a slice of the SQL ARRAY value designated by this Array object, beginning with the specified index and containing up to count successive elements of the SQL array.
Retrieves a result set that contains the elements of the SQL ARRAY value designated by this Array object.
Retrieves a result set holding the elements of the subarray that starts at index index and contains up to count successive elements.
Retrieves a result set holding the elements of the subarray that starts at index index and contains up to count successive elements.
Retrieves a result set that contains the elements of the SQL ARRAY value designated by this Array object.
Method Details
getBaseTypeName
Retrieves the SQL type name of the elements in the array designated by this Array object. If the elements are a built-in type, it returns the database-specific type name of the elements. If the elements are a user-defined type (UDT), this method returns the fully-qualified SQL type name.
getBaseType
getArray
Retrieves the contents of the SQL ARRAY value designated by this Array object in the form of an array in the Java programming language. This version of the method getArray uses the type map associated with the connection for customizations of the type mappings. Note: When getArray is used to materialize a base type that maps to a primitive data type, then it is implementation-defined whether the array returned is an array of that primitive data type or an array of Object .
getArray
Retrieves the contents of the SQL ARRAY value designated by this Array object. This method uses the specified map for type map customizations unless the base type of the array does not match a user-defined type in map , in which case it uses the standard mapping. This version of the method getArray uses either the given type map or the standard mapping; it never uses the type map associated with the connection. Note: When getArray is used to materialize a base type that maps to a primitive data type, then it is implementation-defined whether the array returned is an array of that primitive data type or an array of Object .
getArray
Retrieves a slice of the SQL ARRAY value designated by this Array object, beginning with the specified index and containing up to count successive elements of the SQL array. This method uses the type map associated with the connection for customizations of the type mappings. Note: When getArray is used to materialize a base type that maps to a primitive data type, then it is implementation-defined whether the array returned is an array of that primitive data type or an array of Object .
getArray
Retrieves a slice of the SQL ARRAY value designated by this Array object, beginning with the specified index and containing up to count successive elements of the SQL array. This method uses the specified map for type map customizations unless the base type of the array does not match a user-defined type in map , in which case it uses the standard mapping. This version of the method getArray uses either the given type map or the standard mapping; it never uses the type map associated with the connection. Note: When getArray is used to materialize a base type that maps to a primitive data type, then it is implementation-defined whether the array returned is an array of that primitive data type or an array of Object .
getResultSet
Retrieves a result set that contains the elements of the SQL ARRAY value designated by this Array object. If appropriate, the elements of the array are mapped using the connection’s type map; otherwise, the standard mapping is used. The result set contains one row for each array element, with two columns in each row. The second column stores the element value; the first column stores the index into the array for that element (with the first array element being at index 1). The rows are in ascending order corresponding to the order of the indices.
getResultSet
Retrieves a result set that contains the elements of the SQL ARRAY value designated by this Array object. This method uses the specified map for type map customizations unless the base type of the array does not match a user-defined type in map , in which case it uses the standard mapping. This version of the method getResultSet uses either the given type map or the standard mapping; it never uses the type map associated with the connection. The result set contains one row for each array element, with two columns in each row. The second column stores the element value; the first column stores the index into the array for that element (with the first array element being at index 1). The rows are in ascending order corresponding to the order of the indices.
getResultSet
Retrieves a result set holding the elements of the subarray that starts at index index and contains up to count successive elements. This method uses the connection’s type map to map the elements of the array if the map contains an entry for the base type. Otherwise, the standard mapping is used. The result set has one row for each element of the SQL array designated by this object, with the first row containing the element at index index . The result set has up to count rows in ascending order based on the indices. Each row has two columns: The second column stores the element value; the first column stores the index into the array for that element.
getResultSet
Retrieves a result set holding the elements of the subarray that starts at index index and contains up to count successive elements. This method uses the specified map for type map customizations unless the base type of the array does not match a user-defined type in map , in which case it uses the standard mapping. This version of the method getResultSet uses either the given type map or the standard mapping; it never uses the type map associated with the connection. The result set has one row for each element of the SQL array designated by this object, with the first row containing the element at index index . The result set has up to count rows in ascending order based on the indices. Each row has two columns: The second column stores the element value; the first column stores the index into the array for that element.
free
This method frees the Array object and releases the resources that it holds. The object is invalid once the free method is called. After free has been called, any attempt to invoke a method other than free will result in a SQLException being thrown. If free is called multiple times, the subsequent calls to free are treated as a no-op.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.Using Array Objects
Note: MySQL and Java DB currently do not support the ARRAY SQL data type. Consequently, no JDBC tutorial example is available to demonstrate the Array JDBC data type.
The following topics are covered:
Creating Array Objects
Use the method Connection.createArrayOf to create Array objects.
For example, suppose your database contains a table named REGIONS , which has been created and populated with the following SQL statements; note that the syntax of these statements will vary depending on your database:
create table REGIONS (REGION_NAME varchar(32) NOT NULL, ZIPS varchar32 ARRAY[10] NOT NULL, PRIMARY KEY (REGION_NAME)); insert into REGIONS values( 'Northwest', ''); insert into REGIONS values( 'Southwest', '');
Connection con = DriverManager.getConnection(url, props); String [] northEastRegion = < "10022", "02110", "07399" >; Array anArray = con.createArrayOf("VARCHAR", northEastRegion);
The Oracle Database JDBC driver implements the java.sql.Array interface with the oracle.sql.ARRAY class.
Retrieving and Accessing Array Values in ResultSet
As with the JDBC 4.0 large object interfaces ( Blob , Clob , NClob ), you can manipulate Array objects without having to bring all of their data from the database server to your client computer. An Array object materializes the SQL ARRAY it represents as either a result set or a Java array.
The following excerpt retrieves the SQL ARRAY value in the column ZIPS and assigns it to the java.sql.Array object z object. The excerpt retrieves the contents of z and stores it in zips , a Java array that contains objects of type String . The excerpt iterates through the zips array and checks that each postal (zip) code is valid. This code assumes that the class ZipCode has been defined previously with the method isValid returning true if the given zip code matches one of the zip codes in a master list of valid zip codes:
ResultSet rs = stmt.executeQuery( "SELECT region_name, zips FROM REGIONS"); while (rs.next()) < Array z = rs.getArray("ZIPS"); String[] zips = (String[])z.getArray(); for (int i = 0; i < zips.length; i++) < if (!ZipCode.isValid(zips[i])) < // . // Code to display warning >> >
In the following statement, the ResultSet method getArray returns the value stored in the column ZIPS of the current row as the java.sql.Array object z :
The variable z contains a locator, which is a logical pointer to the SQL ARRAY on the server; it does not contain the elements of the ARRAY itself. Being a logical pointer, z can be used to manipulate the array on the server.
In the following line, getArray is the Array.getArray method, not the ResultSet.getArray method used in the previous line. Because the Array.getArray method returns an Object in the Java programming language and because each zip code is a String object, the result is cast to an array of String objects before being assigned to the variable zips .
String[] zips = (String[])z.getArray();
The Array.getArray method materializes the SQL ARRAY elements on the client as an array of String objects. Because, in effect, the variable zips contains the elements of the array, it is possible to iterate through zips in a for loop, looking for zip codes that are not valid.
Storing and Updating Array Objects
Use the methods PreparedStatement.setArray and PreparedStatement.setObject to pass an Array value as an input parameter to a PreparedStatement object.
The following example sets the Array object anArray (created in a previous example) as the second parameter to the PreparedStatement pstmt :
PreparedStatement pstmt = con.prepareStatement( "insert into REGIONS (region_name, zips) " + "VALUES (?, ?)"); pstmt.setString(1, "NorthEast"); pstmt.setArray(2, anArray); pstmt.executeUpdate();
Similarly, use the methods PreparedStatement.updateArray and PreparedStatement.updateObject to update a column in a table with an Array value.
Releasing Array Resources
Array objects remain valid for at least the duration of the transaction in which they are created. This could potentially result in an application running out of resources during a long running transaction. Applications may release Array resources by invoking their free method.
In the following excerpt, the method Array.free is called to release the resources held for a previously created Array object.
Array aArray = con.createArrayOf("VARCHAR", northEastRegionnewYork); // . aArray.free();