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和JavaDB目前不支持RowidJDBC接口。RowId JDBC interface. Consequently, no JDBC tutorial example is available to demonstrate the features described in this section.因此,没有JDBC教程示例可用于演示本节中描述的功能。
A RowId object represents an address to a row in a database table. RowId对象表示数据库表中某一行的地址。Note, however, that the 但是,请注意,ROWID type is not a standard SQL type. ROWID类型不是标准的SQL类型。ROWID values can be useful because they are typically the fastest way to access a single row and are unique identifies for rows in a table. ROWID值非常有用,因为它们通常是访问单行的最快方式,并且是表中行的唯一标识。However, you should not use a 但是,不应将ROWID value as the primary key of a table. ROWID值用作表的主键。For example, if you delete a particular row from a table, a database might reassign its 例如,如果从表中删除特定行,则数据库可能会将其ROWID value to a row inserted later.ROWID值重新分配给以后插入的行。
The following topics are covered:涵盖以下主题:
Retrieve a 通过调用接口java.sql.RowId object by calling the getter methods defined in the interfaces ResultSet and CallableStatement. ResultSet和CallableStatement中定义的getter方法检索java.sql.RowId对象。The 返回的RowId object that is returned is an immutable object that you can use for subsequent referrals as a unique identifier to a row. RowId对象是一个不可变的对象,您可以将其作为行的唯一标识符用于后续引用。The following is an example of calling the 以下是调用ResultSet.getRowId method:ResultSet.getRowId方法的示例:
java.sql.RowId rowId_1 = rs.getRowId(1);
You can set a 可以将RowId object as a parameter in a parameterized PreparedStatement object:RowId对象设置为参数化PreparedStatement对象中的参数:
Connection conn = ds.getConnection(username, password);
PreparedStatement ps = conn.prepareStatement(
"INSERT INTO BOOKLIST" +
"(ID, AUTHOR, TITLE, ISBN) " +
"VALUES (?, ?, ?, ?)");
ps.setRowId(1, rowId_1);You can also update a column with a specific 还可以使用可更新的RowId object in an updatable ResultSet object:ResultSet对象中的特定RowId对象更新列:
ResultSet rs = ... rs.next(); rs.updateRowId(1, rowId_1);
A RowId object value is typically not portable between data sources and should be considered as specific to the data source when using the set or update method in PreparedStatement and ResultSet objects, respectively. RowId对象值通常不能在数据源之间移植,当分别在PreparedStatement和ResultSet对象中使用set或update方法时,应将其视为特定于数据源。It is therefore inadvisable to get a 因此,不建议从连接到一个数据源的RowId object from a ResultSet object with a connection to one data source and then attempt to use the same RowId object in a unrelated ResultSet object with a connection to a different data source.ResultSet对象中获取RowId对象,然后尝试在连接到不同数据源的不相关ResultSet对象中使用相同的RowId对象。
A 只要标识的行未被删除,并且RowId object is valid as long as the identified row is not deleted and the lifetime of the RowId object is within the bounds of the lifetime specified by that the data source for the RowId.RowId对象的生存期在该数据源为RowId指定的生存期的范围内,RowId对象就是有效的。
To determine the lifetime of 要确定数据库或数据源的RowId objects of your database or data source, call the method DatabaseMetaData.getRowIdLifetime. RowId对象的生存期,请调用方法DatabaseMetaData.getRowIdLifetime。It returns a value of a 它返回RowIdLifetime enumerated data type. RowIdLifetime枚举数据类型的值。The following method 以下方法JDBCTutorialUtilities.rowIdLifeTime returns the lifetime of RowId objects:JDBCTutorialUtilities.rowIdLifeTime返回RowId对象的生存期:
public static void rowIdLifetime(Connection conn)
throws SQLException {
DatabaseMetaData dbMetaData = conn.getMetaData();
RowIdLifetime lifetime = dbMetaData.getRowIdLifetime();
switch (lifetime) {
case ROWID_UNSUPPORTED:
System.out.println("ROWID type not supported");
break;
case ROWID_VALID_FOREVER:
System.out.println("ROWID has unlimited lifetime");
break;
case ROWID_VALID_OTHER:
System.out.println("ROWID has indeterminate lifetime");
break;
case ROWID_VALID_SESSION:
System.out.println(
"ROWID type has lifetime that " +
"is valid for at least the " +
"containing session");
break;
case ROWID_VALID_TRANSACTION:
System.out.println(
"ROWID type has lifetime that " +
"is valid for at least the " +
"containing transaction");
break;
}
}