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

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** Connect to the specified table on the specified
 *  host and retrieve/display the entire table.
 *  Java 1.1 only.
 */

public class ShowTable extends CloseableFrame
                       implements ActionListener {
  public static void main(String[] args) {
    new ShowTable("ShowTable");
  }

  protected LabeledTextField tableField, hostField,
                             dbNameField;
  protected Button showTableButton;
  protected Panel inputPanel, tablePanel;
  protected Connection connection;
  protected ResultSetMetaData metaData;
  
  public ShowTable(String title) {
    super(title);
    inputPanel = makeInputPanel();
    add("North", inputPanel);
    pack();
    setVisible(true);
  }

  /** When the user clicks the "Show Table" button,
   *  the specified table is retrieved and 
   *  a Panel is created to hold the results.
   */

  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == showTableButton) {
      makeConnection();
      invalidate();
      if (tablePanel != null)
        remove(tablePanel);
      tablePanel = makeTablePanel();
      add("Center", tablePanel);
      pack();
      validate();
    }
  }

  // Connect to the specified host and database
  // using the Connect SW driver and a preset
  // username and password.
                         
  private void makeConnection() {
    String driver = "connect.microsoft.MicrosoftDriver";
    try {
      Class.forName(driver);
      String host = hostField.getTextField().getText();
      String url =
        "jdbc:ff-microsoft://" + host + ":1433/" +
        dbNameField.getTextField().getText();
      String user = "sa", password="";
      connection =
        DriverManager.getConnection(url, user, password);
      Statement statement = connection.createStatement();
    } catch(ClassNotFoundException cnfe) {
      System.out.println("No such class: " + driver);
    } catch(SQLException se) {
      System.out.println("SQLException: " + se);
    }
  }

  // Create a Panel that holds the textfields that
  // gather user input, plus the "Show Table" button.
                         
  private Panel makeInputPanel() {
    Panel inputPanel = new Panel();
    inputPanel.setBackground(Color.lightGray);
    hostField = new LabeledTextField("Host Name:", 20);
    inputPanel.add(hostField);
    dbNameField = new LabeledTextField("DB Name:", 10);
    inputPanel.add(dbNameField);
    tableField = new LabeledTextField("Table Name:", 15);
    inputPanel.add(tableField);
    showTableButton = new Button("Show Table");
    inputPanel.add(showTableButton);
    showTableButton.addActionListener(this);
    return(inputPanel);
  }

  // Create a Panel with one TextField for each table
  // entry, plus an extra row for the column names.
                         
  private Panel makeTablePanel() {
    Panel tablePanel = new Panel();
    tablePanel.setBackground(Color.white);
    try {
      // Create statement
      Statement statement = connection.createStatement();
      // Lookup table name
      String table = tableField.getTextField().getText();
      // Ask for everything in that table
      ResultSet results =
        statement.executeQuery("SELECT * FROM " + table);
      // Get MetaData that determines number of columns
      // and column names
      metaData = 
        results.getMetaData();
      int cols = metaData.getColumnCount();
      // Layout Panel to hold specified number of
      // columns and whatever number of rows is needed
      tablePanel.setLayout(new GridLayout(0, cols));
      // A boldface textfield for each column name
      TextField field;
      Font headerFont =
        new Font("SansSerif", Font.BOLD, 14);
      for(int i=1; i<=cols; i++) {
        field = new TextField(metaData.getColumnName(i));
        field.setFont(headerFont);
        tablePanel.add(field);
      }
      // A regular-face textfield for each table entry
      Font bodyFont =
        new Font("Serif", Font.PLAIN, 12);
      String value;
      while(results.next())
        for(int i=1; i<=cols; i++) {
          value = results.getString(i);
          if (value == null)
            value = "<null>";
          field = new TextField(value);
          field.setFont(bodyFont);
          tablePanel.add(field);
        }
    } catch(SQLException se) {
      System.out.println("SQL Exception: " + se);
    }
    return(tablePanel);
  }
}

