Documentation

The Java™ Tutorials
Hide TOC
Using RowId Objects使用RowId对象
Trail: JDBC Database Access
Lesson: JDBC Basics

Using RowId Objects使用RowId对象

Note: MySQL and Java DB currently do not support the RowId JDBC interface. 注意:MySQL和JavaDB目前不支持RowidJDBC接口。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:涵盖以下主题:

Retrieving RowId Objects检索RowId对象

Retrieve a java.sql.RowId object by calling the getter methods defined in the interfaces ResultSet and CallableStatement. 通过调用接口ResultSetCallableStatement中定义的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);

Using RowId Objects使用RowId对象

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对象值通常不能在数据源之间移植,当分别在PreparedStatementResultSet对象中使用setupdate方法时,应将其视为特定于数据源。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对象。

Lifetime of RowId ValidityRowId有效期

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.getRowIdLifetimeIt 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;
    }
}

Previous page: Using Datalink Objects
Next page: Using Stored Procedures