Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

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 (Continued)

Line 26 uses Sun’s reference implementation of the JdbcRowSet interface, JdbcRowSetImpl from package com.sun.rowset, to create a JdbcRowSet object. Package com.sun.rowset provides implementations of the interfaces in package javax.sql.rowset. We used class JdbcRowSetImpl here to demonstrate the capability of the JdbcRowSet interface. Some databases may provide their own RowSet implementations.

Line 27 invokes JdbcRowSet method setUrl to specify the database URL, which is then used by the DriverManager to establish a connection. Line 28 invokes JdbcRowSet method setUsername to specify the username, which is then used by the DriverManagger to establish a connection. Line 29 invokes JdbcRowSet method setPassword to specify the password, which is then used by the DriverManager to establish a connection. Line 30 invokes JdbcRowSet method setCommand to specify the SQL query. Line 31 invokes JdbcRowSet method execute to execute the SQL query. Method execute performs four actions—it establishes a Connection, prepares the query Statement, executes the query and stores the ResultSet returned by query. The Connection, Statement and ResultSet are encapsulated in the JdbcRowSet object. The remaining code is almost identical to Fig. 25.25, except that line 34 obtains a ResultSetMetaData object from the JdbcRowSet, line 44 uses the JdbcRowSet’s next method to get the next row of the result, and line 47uses the JdbcRowSet’s getObject method to obtain a column’s value. Note that the output of this application is exactly the same as that of Fig. 25.25

Page 1 | 2 | 3

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

Java RowSet Tutorial: RowSet Interface

Tutorial Index