The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Note: MySQL and Java DB currently do not support the 注意:MySQL和Java DB目前不支持数组SQL数据类型。ARRAY
SQL data type. Consequently, no JDBC tutorial example is available to demonstrate the 因此,没有JDBC教程示例可用于演示Array
JDBC data type.Array
JDBC数据类型。
The following topics are covered:涵盖以下主题:
Use the method 使用方法Connection.createArrayOf
to create Array
objects.Connection.createArrayOf
创建Array
对象。
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:REGIONS
的表,该表是用以下SQL语句创建和填充的;请注意,这些语句的语法因数据库而异:
create table REGIONS (REGION_NAME varchar(32) NOT NULL, ZIPS varchar32 ARRAY[10] NOT NULL, PRIMARY KEY (REGION_NAME)); insert into REGIONS values( 'Northwest', '{"93101", "97201", "99210"}'); insert into REGIONS values( 'Southwest', '{"94105", "90049", "92027"}');
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.
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
:
Array z = rs.getArray("ZIPS");
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.
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.
Array
objects remain valid for at least the duration of the transaction in which they are created. Array
对象至少在创建它们的事务期间保持有效。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.free
方法来释放Array
资源。
In the following excerpt, the method 在下面的摘录中,调用方法Array.free
is called to release the resources held for a previously created Array
object.Array.free
来释放为先前创建的Array
对象保留的资源。
Array aArray = con.createArrayOf("VARCHAR", northEastRegionnewYork); // ... aArray.free();