Documentation

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

Using RowSet Objects使用行集对象

A JDBC RowSet object holds tabular data in a way that makes it more flexible and easier to use than a result set.JDBCRowSet对象以一种比结果集更灵活、更易于使用的方式保存表格数据。

Oracle has defined five RowSet interfaces for some of the more popular uses of a RowSet, and standard reference are available for these RowSet interfaces. Oracle为RowSet的一些更流行的用途定义了五个RowSet接口,这些RowSet接口提供了标准参考。In this tutorial you will learn how to use these reference implementations.在本教程中,您将学习如何使用这些参考实现。

These versions of the RowSet interface and their implementations have been provided as a convenience for programmers. 为方便程序员,提供了这些版本的RowSet接口及其实现。Programmers are free to write their own versions of the javax.sql.RowSet interface, to extend the implementations of the five RowSet interfaces, or to write their own implementations. 程序员可以自由编写自己版本的javax.sql.RowSet接口,扩展五个RowSet接口的实现,或者编写自己的实现。However, many programmers will probably find that the standard reference implementations already fit their needs and will use them as is.然而,许多程序员可能会发现标准参考实现已经满足了他们的需求,并将按原样使用它们。

This section introduces you to the RowSet interface and the following interfaces that extend this interface:本节向您介绍RowSet接口以及扩展此接口的以下接口:

The following topics are covered:涵盖以下主题:

What Can RowSet Objects Do?行集对象可以做什么?

All RowSet objects are derived from the ResultSet interface and therefore share its capabilities. 所有RowSet对象都派生自ResultSet接口,因此共享其功能。What makes JDBC RowSet objects special is that they add these new capabilities:JDBCRowSet对象的特殊之处在于它们添加了以下新功能:

Function as JavaBeans Component作为JavaBeans组件的函数

All RowSet objects are JavaBeans components. 所有RowSet对象都是JavaBeans组件。This means that they have the following:这意味着他们具备以下条件:

Properties性质

All RowSet objects have properties. 所有RowSet对象都具有属性。A property is a field that has corresponding getter and setter methods. 属性是具有相应的getter和setter方法的字段。Properties are exposed to builder tools (such as those that come with the IDEs JDveloper and Eclipse) that enable you to visually manipulate beans. 属性向构建器工具(例如IDE JDDeveloper和Eclipse附带的工具)公开,这些工具使您能够直观地操作bean。For more information, see the Properties lesson in the JavaBeans trail.有关更多信息,请参阅JavaBeans教程中的属性课程。

JavaBeans Notification MechanismJavaBeans通知机制

RowSet objects use the JavaBeans event model, in which registered components are notified when certain events occur. RowSet对象使用JavaBeans事件模型,在该模型中,当某些事件发生时,会通知已注册的组件。For all RowSet objects, three events trigger notifications:对于所有RowSet对象,有三个事件触发通知:

The notification of an event goes to all listeners, components that have implemented the RowSetListener interface and have had themselves added to the RowSet object's list of components to be notified when any of the three events occurs.事件的通知将发送给所有侦听器,这些侦听器是已实现RowSetListener接口并已将其自身添加到RowSet对象的组件列表中的组件,这些组件将在三个事件中的任何一个发生时收到通知。

A listener could be a GUI component such as a bar graph. 侦听器可以是GUI组件,如条形图。If the bar graph is tracking data in a RowSet object, the listener would want to know the new data values whenever the data changed. 如果条形图正在跟踪RowSet对象中的数据,则侦听器希望在数据更改时了解新的数据值。The listener would therefore implement the RowSetListener methods to define what it will do when a particular event occurs. 因此,侦听器将实现RowSetListener方法,以定义在特定事件发生时它将执行的操作。Then the listener also must be added to the RowSet object's list of listeners. 然后,还必须将侦听器添加到RowSet对象的侦听器列表中。The following line of code registers the bar graph component bg with the RowSet object rs.以下代码行将条形图组件bg注册到RowSet对象rs

rs.addListener(bg);

Now bg will be notified each time the cursor moves, a row is changed, or all of rs gets new data.现在,每当游标移动、行更改或所有rs获得新数据时,bg都会收到通知。

Add Scrollability or Updatability添加可滚动性或可更新性

Some DBMSs do not support result sets that can be scrolled (scrollable), and some do not support result sets that can be updated (updatable). 有些DBMS不支持可滚动(可滚动)的结果集,有些DBMS不支持可更新(可更新)的结果集。If a driver for that DBMS does not add the ability to scroll or update result sets, you can use a RowSet object to do it. 如果该DBMS的驱动程序没有添加滚动或更新结果集的功能,则可以使用RowSet对象来执行此操作。A RowSet object is scrollable and updatable by default, so by populating a RowSet object with the contents of a result set, you can effectively make the result set scrollable and updatable.默认情况下,RowSet对象是可滚动和可更新的,因此通过使用结果集的内容填充行集对象,可以有效地使结果集可滚动和可更新。

Kinds of RowSet Objects行集对象的种类

A RowSet object is considered either connected or disconnected. RowSet对象被视为已连接或已断开连接。A connected RowSet object uses a JDBC driver to make a connection to a relational database and maintains that connection throughout its life span. 已连接的RowSet对象使用JDBC驱动程序与关系数据库建立连接,并在其整个生命周期内维护该连接。A disconnected RowSet object makes a connection to a data source only to read in data from a ResultSet object or to write data back to the data source. 断开连接的RowSet对象与数据源建立连接,只是为了从ResultSet对象读入数据或将数据写回数据源。After reading data from or writing data to its data source, the RowSet object disconnects from it, thus becoming "disconnected." 在从其数据源读取数据或将数据写入数据源后,RowSet对象将与其断开连接,从而变为“断开连接”。During much of its life span, a disconnected RowSet object has no connection to its data source and operates independently. 在其生命周期的大部分时间内,断开连接的RowSet对象与其数据源没有连接,并且独立运行。The next two sections tell you what being connected or disconnected means in terms of what a RowSet object can do.接下来的两部分将告诉您,就RowSet对象的功能而言,连接或断开连接意味着什么。

Connected RowSet Objects连接的行集对象

Only one of the standard RowSet implementations is a connected RowSet object: JdbcRowSet. 标准RowSet实现中只有一个是连接的RowSet对象:JdbcRowSetAlways being connected to a database, a JdbcRowSet object is most similar to a ResultSet object and is often used as a wrapper to make an otherwise non-scrollable and read-only ResultSet object scrollable and updatable.JdbcRowSet对象始终连接到数据库,它与ResultSet对象最为相似,通常用作包装器,以使原本不可滚动且只读的ResultSet对象可滚动且可更新。

As a JavaBeans component, a JdbcRowSet object can be used, for example, in a GUI tool to select a JDBC driver. 作为JavaBeans组件,例如,可以在GUI工具中使用JdbcRowSet对象来选择JDBC驱动程序。A JdbcRowSet object can be used this way because it is effectively a wrapper for the driver that obtained its connection to the database.JdbcRowSet对象可以这样使用,因为它实际上是获得数据库连接的驱动程序的包装器。

Disconnected RowSet Objects断开连接的行集对象

The other four implementations are disconnected RowSet implementations. 其他四种实现是断开连接的RowSet实现。Disconnected RowSet objects have all the capabilities of connected RowSet objects plus they have the additional capabilities available only to disconnected RowSet objects. 断开连接的RowSet对象具有连接的RowSet对象的所有功能,并且它们具有仅对断开连接的RowSet对象可用的附加功能。For example, not having to maintain a connection to a data source makes disconnected RowSet objects far more lightweight than a JdbcRowSet object or a ResultSet object. 例如,不必维护与数据源的连接,使断开连接的RowSet对象比JdbcRowSet对象或ResultSet对象轻得多。Disconnected RowSet objects are also serializable, and the combination of being both serializable and lightweight makes them ideal for sending data over a network. 断开连接的RowSet对象也是可序列化的,可序列化和轻量级的结合使它们非常适合通过网络发送数据。They can even be used for sending data to thin clients such as PDAs and mobile phones.它们甚至可以用于向PDA和移动电话等轻客户端发送数据。

The CachedRowSet interface defines the basic capabilities available to all disconnected RowSet objects. CachedRowSet接口定义了所有断开连接的RowSet对象可用的基本功能。The other three are extensions of the CachedRowSet interface, which provide more specialized capabilities. 另外三个是CachedRowSet接口的扩展,它提供了更专门的功能。The following information shows how they are related:以下信息显示了它们之间的关系:

A CachedRowSet object has all the capabilities of a JdbcRowSet object plus it can also do the following:CachedRowSet对象具有JdbcRowSet对象的所有功能,并且还可以执行以下操作:

A WebRowSet object has all the capabilities of a CachedRowSet object plus it can also do the following:WebRowSet对象具有CachedRowSet对象的所有功能,并且还可以执行以下操作:

A JoinRowSet object has all the capabilities of a WebRowSet object (and therefore also those of a CachedRowSet object) plus it can also do the following:JoinRowSet对象具有WebRowSet对象(因此也具有CachedRowSet对象)的所有功能,此外,它还可以执行以下操作:

A FilteredRowSet object likewise has all the capabilities of a WebRowSet object (and therefore also a CachedRowSet object) plus it can also do the following:FilteredRowSet对象同样具有WebRowSet对象(因此也是CachedRowSet对象)的所有功能,并且还可以执行以下操作:


Previous page: Using Transactions
Next page: Using JdbcRowSet Objects