public class JdbcRowSetTest
classNotFound
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
69:
70:
71: /**************************************************************************
72: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
73: * Pearson Education, Inc. All Rights Reserved. *
74: * *
75: * DISCLAIMER: The authors and publisher of this book have used their *
76: * best efforts in preparing the book. These efforts include the *
77: * development, research, and testing of the theories and programs *
78: * to determine their effectiveness. The authors and publisher make *
79: * no warranty of any kind, expressed or implied, with regard to these *
80: * programs or to the documentation contained in these books. The authors *
81: * and publisher shall not be liable in any event for incidental or *
82: * consequential damages in connection with, or arising out of, the *
83: * furnishing, performance, or use of these programs. *
84: *************************************************************************/