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发行说明。
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:本节涵盖以下主题:
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
实现的方法:
createCachedRowSet
createFilteredRowSet
createJdbcRowSet
createJoinRowSet
createWebRowSet
When you create a 使用JdbcRowSet
object with an instance of RowSetFactory
, the new JdbcRowSet
object will have the following properties:RowSetFactory
实例创建JdbcRowSet
对象时,新的JdbcRowSet
对象将具有以下属性:
type
: ResultSet.TYPE_SCROLL_INSENSITIVE
concurrency
: ResultSet.CONCUR_UPDATABLE
escapeProcessing
: true
maxRows
: 0
maxFieldSize
: 0
BINARY
, VARBINARY
, LONGVARBINARY
, CHAR
, VARCHAR
, and LONGVARCHAR
values)BINARY
、VARBINARY
、LONGVARBINARY
、CHAR
、VARCHAR
和LONGVARCHAR
值的列)queryTimeout
: 0
showDeleted
: false
transactionIsolation
: Connection.TRANSACTION_READ_COMMITTED
typeMap
: null
Connection
object used by this RowSet
object is null
)RowSet
对象使用的Connection
对象关联的类型映射为null
)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
对象都是可滚动和可更新的,除非您为这些属性设置了不同的值。
The section Default JdbcRowSet Objects lists the properties that are set by default when a new “默认JdbcRowSet对象”部分列出了创建新JdbcRowSet
object is created. 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.以下四个属性包含用于获取数据库连接的信息。
username
password
url
datasourceName
DataSource
object that has been registered with a JNDI naming serviceDataSource
对象的名称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
方法在后台为您做很多事情:
url
, username
, and password
properties.url
、username
和password
属性的值与数据库建立连接。command
property.command
属性中设置的查询。ResultSet
object into the jdbcRs
object.ResultSet
对象中的数据读取到jdbcRs
对象中。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:本节涵盖以下主题:
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
将游标移向开头。
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 如果所有者知道Espresso位于jdbcRs
object, the code for doing this might look like the following: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.
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
对象和数据库中。
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();
The sample 示例JdbcRowSetSample
does the following:JdbcRowSetSample
执行以下操作:
JdbcRowSet
object initialized with the ResultSet
object that was produced by the execution of a query that retrieves all the rows in the COFFEES
tableJdbcRowSet
对象,该对象使用ResultSet
对象初始化,ResultSet
对象是通过执行检索COFFEES
表中所有行的查询生成的COFFEES
table and updates the PRICE
column in that rowCoffes
表的第三行,并更新该行中的PRICE
列HouseBlend
and one for HouseDecaf
HouseBlend
,一个用于HouseDecaf