Displaylis.txt

 //  Displaylis.java
// Displaying the contents of the book table.

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

public class Displaylis extends JFrame {

   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
   static final String DATABASE_URL = "jdbc:odbc:lis";

   // declare Connection and Statement for accessing
   // and querying database
   private Connection connection;
   private Statement statement;

   // constructor connects to database, queries database, processes
   // results and displays results in window
   public Displaylis()
   {
      super( "Book Table of lis Database" );

      // connect to database books and query database
      try {


         // load database driver class
         Class.forName( JDBC_DRIVER );

         // establish connection to database
         connection = DriverManager.getConnection( DATABASE_URL );

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

ResultSet resultSet1 =
    statement.executeQuery("SELECT DISTINCT Title, Publisher FROM book");
ResultSet resultSet2 =
    statement.executeQuery("INSERT INTO book VALUES
("+"'Software Engineering Theory and Practice', 'Shari Lawrence Pfleeger', 'Prentice Hall', 0130290491'");
ResultSet resultSet3 =
    statement.executeQuery("DELETE * FROM BOOK WHERE ISBN='764543733'") ;
ResultSet resultSet4 =
      statement.executeQuery("UPDATE BOOK"+SET ISBN = '7232621'+ "WHERE PUBLISHER = 'Addison Wesley' ");
         // process query results
         StringBuffer results = new StringBuffer();
         ResultSetMetaData metaData = resultSet.getMetaData();
         int numberOfColumns = metaData.getColumnCount();

         for ( int i = 1; i <= numberOfColumns; i++ )
            results.append( metaData.getColumnName( i ) + "\t" );

         results.append( "\n" );

         while ( resultSet1.next() ) {

         //   for ( int i = 1; i <= numberOfColumns; i++ )
         //      results.append( resultSet1.getObject( i ) + "\t" );

results.append(resultSet1.getString(1) + "\t");
results.append(resultSet1.getString(2) + "\t");
            results.append( "\n" );
         }


while ( resultSet2.next() ) {

         //   for ( int i = 1; i <= numberOfColumns; i++ )
         //      results.append( resultSet2.getObject( i ) + "\t" );

results.append(resultSet2.getString(1) + "\t");
results.append(resultSet2.getString(2) + "\t");
            results.append( "\n" );
         }



while ( resultSet3.next() ) {

         //   for ( int i = 1; i <= numberOfColumns; i++ )
         //      results.append( resultSet3.getObject( i ) + "\t" );

results.append(resultSet3.getString(1) + "\t");
results.append(resultSet3.getString(2) + "\t");
            results.append( "\n" );
         }


while ( resultSet4.next() ) {

         //   for ( int i = 1; i <= numberOfColumns; i++ )
         //      results.append( resultSet4.getObject( i ) + "\t" );

results.append(resultSet4.getString(1) + "\t");
results.append(resultSet4.getString(2) + "\t");
            results.append( "\n" );
         }






         // set up GUI and display window
         JTextArea textArea = new JTextArea( results.toString() );
         Container container = getContentPane();

         container.add( new JScrollPane( textArea ) );

         setSize( 300, 100 );  // set window size
         setVisible( true );   // display window

      }  // end try

      // detect problems interacting with the database
      catch ( SQLException sqlException ) {
         JOptionPane.showMessageDialog( null, sqlException.getMessage(),
            "Database Error", JOptionPane.ERROR_MESSAGE );

         System.exit( 1 );
      }

      // detect problems loading database driver
      catch ( ClassNotFoundException classNotFound ) {
         JOptionPane.showMessageDialog( null, classNotFound.getMessage(),
            "Driver Not Found", JOptionPane.ERROR_MESSAGE );

         System.exit( 1 );
      }

      // ensure statement and connection are closed properly
      finally {

         try {
            statement.close();
            connection.close();
         }

         // handle exceptions closing statement and connection
         catch ( SQLException sqlException ) {
            JOptionPane.showMessageDialog( null,
               sqlException.getMessage(), "Database Error",
               JOptionPane.ERROR_MESSAGE );

            System.exit( 1 );
         }
      }

   }  // end Displaylis constructor

   // launch the application
   public static void main( String args[] )
   {
      Displaylis window = new Displaylis();
      window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

}  // end class Displaylis


Project Homepage: