Computer Science 122
Assignment 3
Due: September 28, 2004

Next create a frame (window) and display your data in a text area on it.  You should read the data about your products from the database and then append it to the text area.  You can follow the example below for help in creating a frame and appending the data.

import java.awt.event.*;
import java.awt.*;
import java.sql.*;

/* AddressFrame creates a frame with a panel and text area.  It then gets data from a database and displays it in the text area. */
public class AddressFrame extends Frame
{
     private Panel panel;
     private Label areaLabel;
     private TextArea area;
 
     public AddressFrame ()
     {
          super ("Frame");
          setSize (300, 300);
 
          // Add a window closer to the frame.
          addWindowListener (new WindowAdapter()
               {public void windowClosing(WindowEvent e) {System.exit(0);}});
 
          // Get a new panel and set its background color.
          panel = new Panel ();
          panel.setBackground (Color.cyan);
 
          // Get a text area and add it to the panel.
          areaLabel = new Label ("Text Area");
          area = new TextArea (10, 30);
          panel.add (areaLabel); panel.add (area);
          add (panel);
 
          // Add data to the text area.
          Addresses addresses = new Addresses ();
          addresses.displayData(area);
     } // constructor

     // The main method gets a new frame and shows it.
     public static void main (String [] args)
     {
          AddressFrame frame = new AddressFrame ();
          frame.setVisible (true);
     }
} // class ShowFrame0

// Addresses opens a database and then displays the data on the text area.
class Addresses
{
    /* displayData opens a connection to a database, makes a query and gets a result set.  It then displays the data on the text area. */
    public void displayData (TextArea area)
    {
          try
          {
               // Open a connection to the addresses database.
                Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection ("jdbc:odbc:addresses");
 
               // Create a statement and query and get the result set.
               Statement stmt = con.createStatement ();
               String query = "Select * From AddressTable";
               ResultSet rs = stmt.executeQuery (query);
 
               // Get the data from the database and display it on the text area.
               while (rs.next ())
               {
                    area.append (rs.getString ("Name") + '\n');
                    area.append (rs.getString ("Email") + '\n');
                    area.append (rs.getString ("Telephone") + '\n');
                    area.append ("\n");
               }
               con.close ();
          } catch (ClassNotFoundException e){System.out.println ("Database connection exception.");}
            catch (SQLException e) {System.out.println ("SQL Exception");}
       } // displayData
} // Addresses