package chapter33;

// DisplayAuthors.java
// Displaying the contents of the authors table.
import java.sql.*;

public class DisplayAuthors
{
    // launch the application
    public static void main( String args[] )
            throws SQLException, ClassNotFoundException
    {
        // load database driver class
        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

        // establish connection to database
        Connection connection = DriverManager.getConnection( "jdbc:odbc:Books" );

        // create Statement for querying database
        Statement statement = connection.createStatement();

        // query database
        ResultSet resultSet = statement.executeQuery(
                "SELECT authorID, firstName, lastName FROM authors" );

        // process query results
        System.out.println( "Authors Table of Books Database:" );
        System.out.printf( "%s\t%s\t%s\t\n",
                "Author ID", "First Name", "Last Name");
        int numberOfColumns = 3;

        while ( resultSet.next() )
        {
            for ( int i = 1; i <= numberOfColumns; i++ )
                System.out.printf( "%-8s\t", resultSet.getObject( i ) );
            System.out.println();
        } // end while

        // Close the connection
        connection.close();

    } // end main
}  // end class DisplayAuthors
