In this tutorial, we introduce the JDBC RowSet interface and its subinterfaces JdbcRowSet and CachedRowSet that support connected and disconnected database access, respectively. You will learn to connect to a database via class JdbcRowSetImpl--the reference implementation of the JdbcRowSet interface that Sun provides with JDK 5.0. You will also learn to use the JdbcRowSetImpl object to execute a SQL query and obtain its results. This tutorial is intended for students and professionals who are already familiar with Java and basic JDBC capabilities.
To execute the example in this tutorial, you must install JDK 5.0, MySQL and the MySQL Connector/J, then create our books database. A script to help create the database is located in the example download for this article. If you would like to execute this example on your own computer, we provide instructions for setting up MySQL and creating the database.
[Notes: This is an excerpt (Section 25.10) of Chapter 25, Accessing Databases with JDBC, from our best-selling textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. When you purchase this book you also get free access to the Web-based Java Multimedia Cyber Classroom, 6/e, for six months. The Cyber Classroom includes audio descriptions of the examples in Chapters 1-14, solutions to approximately one-half of the end-of-chapter exercises, interactive true/false self-assessment questions and a searchable Web-based e-book. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp. 1224-1227. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
Java RowSet Tutorial: Using the RowSet Interface
In the previous examples, you learned how to query a database by explicitly establishing a Connection to the database, preparing a Statement for querying the database and executing the query. In this section, we demonstrate the RowSet interface, which configures the database connection and prepares query statements automatically. Interface RowSet provides several set methods that allow the programmer to specify the properties needed to establish a connection (such as the database URL, user name and password of the database) and create a Statement (such as a query). Interface RowSet also provides several get methods that return these properties.
RowSet is part of the javax.sql package. Although part of Java 2 Standard Edition, the classes and interfaces of package javax.sql are most frequently used in the context of the Java 2 Platform Enterprise Edition (J2EE). J2EE is used in industry to build substantial distributed applications that often process database data. J2EE is beyond the scope of this book. You can learn more about J2EE by visiting java.sun.com/j2ee/.
There are two types of RowSet objects—connected and disconnected. A connected RowSet object connects to the database once and remains connected until the application terminates. A disconnected RowSet object connects to the database, executes a query to retrieve the data from the database and then closes the connection. A program may change the data in a disconnected RowSet while it is disconnected. Modified data can be updated in the database after a disconnected RowSet reestablishes the connection with the database.
J2SE 5.0 package javax.sql.rowset contains two subinterfaces of RowSet—JdbcRowSet and CachedRowSet. JdbcRowSet, a connected RowSet, acts as a wrapper around a ResultSet object, and allows programmers to scroll through and update the rows in the ResultSet. Recall that by default, a ResultSet object is non-scrollable and read only—you must explicitly set the result-set type constant to TYPE_SCROLL_INSENSITIVE and set the result-set concurrency constant to CONCUR_UPDATABLE to make a ResultSet object scrollable and updatable. A JdbcRowSet object is scrollable and updatable by default. CachedRowSet, a disconnected RowSet, caches the data of a ResultSet in memory and disconnects from the database. Like JdbcRowSet, a CachedRowSet object is scrollable and updatable by default. A CachedRowSet object is also serializable, so it can be passed between Java applications through a network, such as the Internet. However, CachedRowSet has a limitation—the amount of data that can be stored in memory is limited. Besides JdbcRowSet and CachedRowSet, package javax.sql.rowset contains three other subinterfaces of RowSet. For details of these interfaces, visit java.sun.com/j2se/5.0/docs/guide/jdbc/getstart/rowsetImpl.html.
Setup for RowSets Example:
Java RowSet Tutorial, Setup Part 1: Instructions to install MySQL and MySQL Connector/J
Java RowSet Tutorial, Setup Part 2: Instructions on Setting MySQL User Account
Java RowSet Tutorial, Setup Part 3: Creating Database books in MySQL