Deitel & Associates, Inc. Logo

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

ISBN:
0-13-148398-6
© 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)

Figure 25.32 reimplements the example of Fig. 25.25 using a RowSet. Rather than establish the connection and create a Statement explicitly, Fig. 25.32 uses a JdbcRowSet object to create a Connection and a Statement automatically.

1  // Fig. 25.32: JdbcRowSetTest.java
2 // Displaying the contents of the authors table using JdbcRowSet.
3 import java.sql.ResultSetMetaData;
4 import java.sql.SQLException;
5 import javax.sql.rowset.JdbcRowSet;
6 import com.sun.rowset.JdbcRowSetImpl; // Sun’s JdbcRowSet implementation
7
8 public class JdbcRowSetTest
9 {
10 // JDBC driver name and database URL
11 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
12 static final String DATABASE_URL = "jdbc:mysql://localhost/books";
13 static final String USERNAME = "jhtp6";
14 static final String PASSWORD = "jhtp6";
15
16 // constructor connects to database, queries database, processes
17 // results and displays results in window
18 public JdbcRowSetTest()
19 {
20 // connect to database books and query database
21 try
22 {
23 Class.forName( JDBC_DRIVER ); // load database driver class
24
25 // specify properties of JdbcRowSet
26 JdbcRowSet rowSet = new JdbcRowSetImpl();
27 rowSet.setUrl( DATABASE_URL ); // set database URL
28 rowSet.setUsername( USERNAME ); // set username
29 rowSet.setPassword( PASSWORD ); // set password
30 rowSet.setCommand( "SELECT * FROM authors" ); // set query
31 rowSet.execute(); // execute query
32
33 // process query results
34 ResultSetMetaData metaData = rowSet.getMetaData();
35 int numberOfColumns = metaData.getColumnCount();
36 System.out.println( "Authors Table of Books Database:" );
37
38 // display rowset header
39 for ( int i = 1; i <= numberOfColumns; i++ )
40 System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
41 System.out.println();
42
43 // display each row
44 while ( rowSet.next() )
45 {
46 for ( int i = 1; i <= numberOfColumns; i++ )
47 System.out.printf( "%-8s\t", rowSet.getObject( i ) );
48 System.out.println();
49 } // end while
50 } // end try
51 catch ( SQLException sqlException )
52 {
53 sqlException.printStackTrace();
54 System.exit( 1 );
55 } // end catch
56 catch ( ClassNotFoundException classNotFound )
57 {
58 classNotFound.printStackTrace();
59 System.exit( 1 );
60 } // end catch
61 } // end DisplayAuthors constructor
62
63 // launch the application
64 public static void main( String args[] )
65 {
66 JdbcRowSetTest window = new JdbcRowSetTest();
67 } // end main
68 } // end class JdbcRowSetTest
 
Authors Table of Books Database:
authorID firstName lastName
1 Harvey Deitel
2 Paul Deitel
3 Tem Nieto
4 Sean Santry

 Fig. 25.32  Displaying the authors table using JdbcRowSets.