Documentation

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

Using JdbcRowSet Objects使用JdbcRowSet对象

A JdbcRowSet object is an enhanced ResultSet object. JdbcRowSet对象是增强的ResultSet对象。It maintains a connection to its data source, just as a ResultSet object does. 它维护到其数据源的连接,就像ResultSet对象一样。The big difference is that it has a set of properties and a listener notification mechanism that make it a JavaBeans component.最大的区别在于它有一组属性和侦听器通知机制,使其成为JavaBeans组件。

One of the main uses of a JdbcRowSet object is to make a ResultSet object scrollable and updatable when it does not otherwise have those capabilities.JdbcRowSet对象的主要用途之一是使ResultSet对象在其他方面不具备这些功能时可滚动和可更新。

This section covers the following topics:本节涵盖以下主题:

Creating JdbcRowSet Objects创建JdbcRowSet对象

Create a JdbcRowSet object by using an instance of RowSetFactory, which is created from the class RowSetProvider. 使用RowSetFactory实例创建JdbcRowSet对象,该实例是从类RowSetProvider创建的。The following example is from JdbcRowSetSample:以下示例来自JdbcRowSetSample

RowSetFactory factory = RowSetProvider.newFactory();

    try (JdbcRowSet jdbcRs = factory.createJdbcRowSet()) {
      jdbcRs.setUrl(this.settings.urlString);
      jdbcRs.setUsername(this.settings.userName);
      jdbcRs.setPassword(this.settings.password);
      jdbcRs.setCommand("select * from COFFEES");
      jdbcRs.execute();
      // ...

The RowSetFactory interface contains methods to create the different types of RowSet implementations:RowSetFactory接口包含用于创建不同类型的RowSet实现的方法:

Default JdbcRowSet Objects默认JdbcRowSet对象

When you create a JdbcRowSet object with an instance of RowSetFactory, the new JdbcRowSet object will have the following properties:使用RowSetFactory实例创建JdbcRowSet对象时,新的JdbcRowSet对象将具有以下属性:

The main thing you must remember from this list is that a JdbcRowSet and all other RowSet objects are scrollable and updatable unless you set different values for those properties.在此列表中,您必须记住的主要一点是,JdbcRowSet和所有其他RowSet对象都是可滚动和可更新的,除非您为这些属性设置了不同的值。

Setting Properties设置属性

The section Default JdbcRowSet Objects lists the properties that are set by default when a new JdbcRowSet object is created. “默认JdbcRowSet对象”部分列出了创建新JdbcRowSet对象时默认设置的属性。If you use the default constructor, you must set some additional properties before you can populate your new JdbcRowSet object with data.如果使用默认构造函数,则必须先设置一些附加属性,然后才能使用数据填充新的JdbcRowSet对象。

In order to get its data, a JdbcRowSet object first needs to connect to a database. 为了获取数据,JdbcRowSet对象首先需要连接到数据库。The following four properties hold information used in obtaining a connection to a database.以下四个属性包含用于获取数据库连接的信息。

Which of these properties you set depends on how you are going to make a connection. 设置哪些属性取决于如何建立连接。The preferred way is to use a DataSource object, but it may not be practical for you to register a DataSource object with a JNDI naming service, which is generally done by a system administrator. 首选的方法是使用DataSource对象,但向JNDI命名服务注册DataSource对象可能并不实际,这通常由系统管理员完成。Therefore, the code examples all use the DriverManager mechanism to obtain a connection, for which you use the url property and not the datasourceName property.因此,代码示例都使用DriverManager机制来获取连接,对于该连接,您使用url属性而不是datasourceName属性。

Another property that you must set is the command property. 必须设置的另一个属性是command属性。This property is the query that determines what data the JdbcRowSet object will hold. 此属性是确定JdbcRowSet对象将保存哪些数据的查询。For example, the following line of code sets the command property with a query that produces a ResultSet object containing all the data in the table COFFEES:例如,以下代码行使用一个查询设置command属性,该查询生成一个ResultSet对象,其中包含COFFEES表中的所有数据:

jdbcRs.setCommand("select * from COFFEES");

After you have set the command property and the properties necessary for making a connection, you are ready to populate the jdbcRs object with data by calling the execute method.设置了command属性和建立连接所需的属性之后,就可以通过调用execute方法用数据填充jdbcRs对象了。

jdbcRs.execute();

The execute method does many things for you in the background:execute方法在后台为您做很多事情:

Using JdbcRowSet Objects使用JdbcRowSet对象

You update, insert, and delete a row in a JdbcRowSet object the same way you update, insert, and delete a row in an updatable ResultSet object. 更新、插入和删除JdbcRowSet对象中的行的方式与更新、插入和删除可更新ResultSet对象中的行的方式相同。Similarly, you navigate a JdbcRowSet object the same way you navigate a scrollable ResultSet object.类似地,导航JdbcRowSet对象的方式与导航可滚动的ResultSet对象的方式相同。

The Coffee Break chain of coffee houses acquired another chain of coffee houses and now has a legacy database that does not support scrolling or updating of a result set. 咖啡休息连锁咖啡馆收购了另一家咖啡馆连锁店,现在有一个不支持滚动或更新结果集的遗留数据库。In other words, any ResultSet object produced by this legacy database does not have a scrollable cursor, and the data in it cannot be modified. However, by creating a JdbcRowSet object populated with the data from a ResultSet object, you can, in effect, make the ResultSet object scrollable and updatable.

As mentioned previously, a JdbcRowSet object is by default scrollable and updatable. Because its contents are identical to those in a ResultSet object, operating on the JdbcRowSet object is equivalent to operating on the ResultSet object itself. And because a JdbcRowSet object has an ongoing connection to the database, changes it makes to its own data are also made to the data in the database.

This section covers the following topics:本节涵盖以下主题:

Navigating JdbcRowSet Objects导航JdbcRowSet对象

A ResultSet object that is not scrollable can use only the next method to move its cursor forward, and it can move the cursor only forward from the first row to the last row. A default JdbcRowSet object, however, can use all of the cursor movement methods defined in the ResultSet interface.

A JdbcRowSet object can call the method next, and it can also call any of the other ResultSet cursor movement methods. For example, the following lines of code move the cursor to the fourth row in the jdbcRs object and then back to the third row:

jdbcRs.absolute(4);
jdbcRs.previous();

The method previous is analogous to the method next in that it can be used in a while loop to traverse all of the rows in order. The difference is that you must move the cursor to a position after the last row, and previous moves the cursor toward the beginning.不同之处在于,必须将游标移动到最后一行之后的位置,而previous将游标移向开头。

Updating Column Values更新列值

You update data in a JdbcRowSet object the same way you update data in a ResultSet object.更新JdbcRowSet对象中的数据的方式与更新ResultSet对象中的数据的方式相同。

Assume that the Coffee Break owner wants to raise the price for a pound of Espresso coffee. 假设咖啡休息时间的主人想提高一磅浓咖啡的价格。If the owner knows that Espresso is in the third row of the jdbcRs object, the code for doing this might look like the following:如果所有者知道Espresso位于jdbcRs对象的第三行,则执行此操作的代码可能如下所示:

jdbcRs.absolute(3);
jdbcRs.updateFloat("PRICE", 10.99f);
jdbcRs.updateRow();

The code moves the cursor to the third row and changes the value for the column PRICE to 10.99, and then updates the database with the new price.代码将游标移动到第三行,并将列PRICE的值更改为10.99,然后使用新的价格更新数据库。

Calling the method updateRow updates the database because jdbcRs has maintained its connection to the database. For disconnected RowSet objects, the situation is different.

Inserting Rows插入行

If the owner of the Coffee Break chain wants to add one or more coffees to what he offers, the owner will need to add one row to the COFFEES table for each new coffee, as is done in the following code fragment from JdbcRowSetSample. Notice that because the jdbcRs object is always connected to the database, inserting a row into a JdbcRowSet object is the same as inserting a row into a ResultSet object: You move to the cursor to the insert row, use the appropriate updater method to set a value for each column, and call the method insertRow:

jdbcRs.moveToInsertRow();
jdbcRs.updateString("COF_NAME", "HouseBlend");
jdbcRs.updateInt("SUP_ID", 49);
jdbcRs.updateFloat("PRICE", 7.99f);
jdbcRs.updateInt("SALES", 0);
jdbcRs.updateInt("TOTAL", 0);
jdbcRs.insertRow();

jdbcRs.moveToInsertRow();
jdbcRs.updateString("COF_NAME", "HouseDecaf");
jdbcRs.updateInt("SUP_ID", 49);
jdbcRs.updateFloat("PRICE", 8.99f);
jdbcRs.updateInt("SALES", 0);
jdbcRs.updateInt("TOTAL", 0);
jdbcRs.insertRow();

When you call the method insertRow, the new row is inserted into the jdbcRs object and is also inserted into the database. 调用insertRow方法时,新行将插入jdbcRs对象,并插入数据库。The preceding code fragment goes through this process twice, so two new rows are inserted into the jdbcRs object and the database.前面的代码片段经历了两次这个过程,因此将两个新行插入到jdbcRs对象和数据库中。

Deleting Rows删除行

As is true with updating data and inserting a new row, deleting a row is just the same for a JdbcRowSet object as for a ResultSet object. 与更新数据和插入新行一样,删除一行对于JdbcRowSet对象和删除ResultSet对象是一样的。The owner wants to discontinue selling French Roast decaffeinated coffee, which is the last row in the jdbcRs object. 店主希望停止销售法国烤无咖啡因咖啡,这是jdbcRs对象的最后一行。In the following lines of code, the first line moves the cursor to the last row, and the second line deletes the last row from the jdbcRs object and from the database:在以下代码行中,第一行将游标移动到最后一行,第二行从jdbcRs对象和数据库中删除最后一行:

jdbcRs.last();
jdbcRs.deleteRow();

Code Sample代码示例

The sample JdbcRowSetSample does the following:示例JdbcRowSetSample执行以下操作:


Previous page: Using RowSet Objects
Next page: Using CachedRowSetObjects