Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.
An address book, for example, might use this capability.
Adding extra capabilities to ResultSets
Offline capabilities are not something that you want every ResultSet implementation to have. The large overheads dealing with serialization and the non−connected status just aren't worth it for most applications.
Therefore, the JDBC designers decided to use another class to represent offline ResultSet information —
RowSet.
In order to work in both environments, the RowSet interface extends the normal ResultSet and adds even more methods. The major extra new functionality is to make the RowSet look like a JavaBean. Set methods now exist for setting the value of the command parameters used to fill the class with data. These do not perform the same task as the update methods that change the contents of the data once the class has been filled. The getters already exist in the base interface so there was no need to add them. The new getter methods are primarily aimed at the underlying database that needs to read the values back in and re−establish any internal database connections. As all JavaBeans require listeners for changes in the bean, new methods have been added to add and remove listeners, and a listener interface — RowSetListener — has been added for changes in the database.
Note As the listeners are primarily designed for UI tool implementors, we won't cover them in this book.
One of the purposes of the RowSet interfaces is to provide a way to transparently pass around database results without needing a database. To accomplish this, the interface is made serializable, and two more interfaces are added: RowSetWriter and RowSetReader. These interfaces are used internally by the RowSet to read and write itself.
Caution
Although the interfaces and the intent of row set are good, their current implementation leaves a lot to be desired. One of their biggest drawbacks is that they are defined as a collection of disconnected interfaces that do not fit the rest of the JDBC API. So even though you have now an instance to play with, you still cannot treat it generically as an interface. You must know which explicit implementation you are playing with. Also, the purpose of the accompanying interfaces for reading and writing is very unclear. The API specification and tutorials you can find on the Internet never address these interfaces and how they fit into the general philosophy. This makes it very hard to recommend their use in current applications.
Filling a RowSet with information
Because the RowSet is an interface, you will need to find a concrete implementation for your use. As a specification, JDBC does not define how to access an instance of the RowSet. Unlike normal database connections, each RowSet is a separate instance, so you can't just use JNDI to locate one for you to make queries of. Each time you want a new row set you must directly instantiate the concrete instance. In terms of portability of code, that is a major problem. While it is possible to write your own implementation, it is a huge class, and we certainly don't recommend it.
Tip You can find a sample implementation of a RowSet from Sun's JDC site. The CachedRowSet can be downloaded from http://developer.java.sun.com/developer/earlyAccess/crs/. You will also find a good tutorial about using row sets within a JSP at
http://developer.java.sun.com/developer/technicalArticles/javaserverpages/cachedrowset/.
132
Chapter 7: Using JDBC to Interact with SQL Databases After creating an instance of the RowSet, you have to populate it with data. To do this, you use a series of commands to set up the initialization parameters and then tell it to fill itself its content based on those parameters. The rough steps you normally follow to fill a RowSet with information are: Set the SQL statement that you want executed to fill the class with data.
1.