public class DisplayAuthors
classNotFound
1: // Fig. 25.25: DisplayAuthors.java
2: // Displaying the contents of the authors table.
3: import java.sql.Connection;
4: import java.sql.Statement;
5: import java.sql.DriverManager;
6: import java.sql.ResultSet;
7: import java.sql.ResultSetMetaData;
8: import java.sql.SQLException;
9:
10: public class DisplayAuthors
11: {
12: // JDBC driver name and database URL
13: static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
14: static final String DATABASE_URL = "jdbc:mysql://localhost/books";
15:
16: // launch the application
17: public static void main( String args[] )
18: {
19: Connection connection = null; // manages connection
20: Statement statement = null; // query statement
21:
22: // connect to database books and query database
23: try
24: {
25: Class.forName( JDBC_DRIVER ); // load database driver class
26:
27: // establish connection to database
28: connection =
29: DriverManager.getConnection( DATABASE_URL, "jhtp6", "jhtp6" );
30:
31: // create Statement for querying database
32: statement = connection.createStatement();
33:
34: // query database
35: ResultSet resultSet = statement.executeQuery(
36: "SELECT authorID, firstName, lastName FROM authors" );
37:
38: // process query results
39: ResultSetMetaData metaData = resultSet.getMetaData();
40: int numberOfColumns = metaData.getColumnCount();
41: System.out.println( "Authors Table of Books Database:" );
42:
43: for ( int i = 1; i <= numberOfColumns; i++ )
44: System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
45: System.out.println();
46:
47: while ( resultSet.next() )
48: {
49: for ( int i = 1; i <= numberOfColumns; i++ )
50: System.out.printf( "%-8s\t", resultSet.getObject( i ) );
51: System.out.println();
52: } // end while
53: } // end try
54: catch ( SQLException sqlException )
55: {
56: sqlException.printStackTrace();
57: System.exit( 1 );
58: } // end catch
59: catch ( ClassNotFoundException classNotFound )
60: {
61: classNotFound.printStackTrace();
62: System.exit( 1 );
63: } // end catch
64: finally // ensure statement and connection are closed properly
65: {
66: try
67: {
68: statement.close();
69: connection.close();
70: } // end try
71: catch ( Exception exception )
72: {
73: exception.printStackTrace();
74: System.exit( 1 );
75: } // end catch
76: } // end finally
77: } // end main
78: } // end class DisplayAuthors
79:
80:
81: /**************************************************************************
82: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
83: * Pearson Education, Inc. All Rights Reserved. *
84: * *
85: * DISCLAIMER: The authors and publisher of this book have used their *
86: * best efforts in preparing the book. These efforts include the *
87: * development, research, and testing of the theories and programs *
88: * to determine their effectiveness. The authors and publisher make *
89: * no warranty of any kind, expressed or implied, with regard to these *
90: * programs or to the documentation contained in these books. The authors *
91: * and publisher shall not be liable in any event for incidental or *
92: * consequential damages in connection with, or arising out of, the *
93: * furnishing, performance, or use of these programs. *
94: *************************************************************************/
95:
96: