home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / DBServ / SAMPLES / SAMPLES.ZIP / CUSTOMER / CUSTOMER.JAVA < prev    next >
Encoding:
Java Source  |  1997-02-21  |  34.1 KB  |  861 lines

  1. /**************************************************************************************************
  2. *                                                                                                 *
  3. *  Class name:  customer                                                                          *
  4. *     Purpose:  This application accesses the customer table and demonstrates basic database      *
  5. *               navigation and functionality (e.g., Add, Save, First, Next)                       *
  6. *                                                                                                 *
  7. *     Imports:  java.awt.*                                                                        *
  8. *               java.applet.*                                                                     *
  9. *               symjava.sql.*                                                                     *
  10. *               java.io.*                                                                         *
  11. *                                                                                                 *
  12. *  Methods include:  void goTo()                                                                  *
  13. *                    boolean goNext()                                                             *
  14. *                    boolean goPrevious()                                                         *
  15. *                    public class customer                                                        *
  16. *                    class DataBase                                                               *
  17. *                    public void init()                                                           *
  18. *                    public boolean handleEvent(Event)                                            *
  19. *                    public void start()                                                          *
  20. *                    public boolean keyDown(Event, int)                                           *
  21. *                    void tab(Event)                                                              *
  22. *                                                                                                 *
  23. * Additional Notes:                                                                               *
  24. * THIS SOFTWARE HAS BEEN COMPILED AND EXECUTED SUCCESSFULLY IN SPECIFIC SYMANTEC                  *
  25. * ENVIRONMENTS, AND IS BEING PROVIDED ONLY AS SAMPLE CODE.                                        *
  26. *                                                                                                 *
  27. * SYMANTEC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,          *
  28. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF               *
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  SYMANTEC SHALL NOT     *
  30. * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING *
  31. * THIS SOFTWARE OR ITS DERIVATIVES.                                                               *
  32. *                                                                                                 *
  33. *  Please see the Sample Code Configuration item in the Read Me file for additional               *
  34. *  information about the sample database used in this applet.                                     *
  35. *                                                                                                 *
  36. * Copyright (c) 1996 Symantec.  All Rights Reserved.                                              *
  37. *                                                                                                 *
  38. ***************************************************************************************************/
  39.  
  40. import java.awt.*;
  41. import java.applet.*;
  42. import symjava.sql.*;
  43. import java.util.*;
  44. import java.io.*;
  45.  
  46. class DataBase {
  47.     private String serverUrl;
  48.     // check for connection.
  49.     private boolean isConnection;
  50.     Driver driver;
  51.     Connection server;
  52.     Properties props;
  53.     ResultSet resultSet;
  54.     int firstRec;
  55.     int lastRec;
  56.     // check if the resultSet is in ascending order w/respect to id
  57.     boolean isAscending;
  58.  
  59.  
  60.     DataBase(String hostname, String user, String password) {
  61.         serverUrl = hostname;
  62.         isConnection = false;
  63.         props = new Properties();
  64.         props.put("user", user);
  65.         props.put("password", password);
  66.         resultSet = null;
  67.         isAscending = true;
  68.         firstRec = 0;
  69.         lastRec = 0;
  70.     }
  71.  
  72.     void connectServer(String driver_name) {
  73.     // Use the driver_name driver to connect to the data base server.
  74.         try {
  75.             driver =
  76.                 (symjava.sql.Driver)Class.forName(driver_name).newInstance();
  77.             server = driver.connect(serverUrl, props);
  78.         }
  79.         catch (SQLException e) {
  80.             System.out.println("Exception from DataBase.connectServer() : " +
  81.                                 e.getMessage());
  82.         }
  83.         catch (InstantiationException e) {
  84.             System.out.println("Exception from DataBase.connectServer() : " +
  85.                                 e.getMessage());
  86.         }
  87.         catch (ClassNotFoundException e) {
  88.             System.out.println("Exception from DataBase.connectServer() : " +
  89.                                 e.getMessage());
  90.         }
  91.         catch (IllegalAccessException e) {
  92.             System.out.println("Exception from DataBase.connectServer() : " +
  93.                                 e.getMessage());
  94.         }
  95.         isConnection = true;
  96.     }
  97.  
  98.     void closeServer() {
  99.     // disconnect with data base server.
  100.         try {
  101.             server.close();
  102.         }
  103.         catch (Exception e){
  104.              System.out.println("Exception from DataBase.closeServer() : " +
  105.                                 e.getMessage());
  106.         }
  107.         isConnection = false;
  108.     }
  109.  
  110.     void setFirst(String rec) {
  111.         try {
  112.                 Integer newFirst = new Integer(rec);
  113.                 firstRec = newFirst.intValue();
  114.         }
  115.         catch(NumberFormatException e) {
  116.                 System.out.println("Exception caught during setFirst() : " + e.getMessage());
  117.         }
  118.     }
  119.  
  120.     void setLast(String rec) {
  121.         try {
  122.                 Integer newLast = new Integer(rec);
  123.                 lastRec = newLast.intValue();
  124.         }
  125.         catch(NumberFormatException e) {
  126.                 System.out.println("Exception caught during setLast() : " + e.getMessage());
  127.         }
  128.     }
  129.  
  130.     void search(String sqlStmt, boolean ascendingOrder) {
  131.     // Execute sqlStmt which will most likely be a "Select * from customer ..." of some
  132.     // sort assigning the resultSet.  ResultSet will contain all records in the table.
  133.     // also change the resultSet ordering flag to ascendingOrder
  134.         search(sqlStmt);
  135.         isAscending = ascendingOrder;
  136.     }
  137.  
  138.     void search(String sqlStmt) {
  139.     // Execute sqlStmt which will most likely be a "Select * from customer ..." of some
  140.     // sort assigning the resultSet.  ResultSet will contain all records in the table.
  141.         try{
  142.             Statement stmt = server.createStatement();
  143.             resultSet = stmt.executeQuery(sqlStmt);
  144.             resultSet.next();
  145.         }
  146.         catch(SQLException e) {
  147.             System.out.println("Exception from DataBase.search() : " +
  148.                                 e.getMessage());
  149.         }
  150.     }
  151.  
  152.     void executeUpdate(String sqlStmt) {
  153.         try {
  154.             Statement stmt = server.createStatement();
  155.             stmt.executeUpdate(sqlStmt);
  156.             stmt.close();
  157.         }
  158.  
  159.         catch(SQLException e) {
  160.             System.out.println("Exception from DataBase.executeUpdate() : " +
  161.                                 e.getMessage());
  162.         }
  163.     }
  164.  
  165.     boolean goNext(String id, boolean isPrevious) {
  166.     // Scroll to the next record either forward or backward.
  167.  
  168.         // strictly for boundary checking
  169.         Integer idValue = null;
  170.         try {
  171.             idValue = new Integer(id);
  172.         }
  173.         catch(NumberFormatException e) {
  174.             System.out.println("Exception caught from goNext() : " + e.getMessage());
  175.         }
  176.  
  177.         int idIntVal = idValue.intValue();
  178.  
  179.         if (( (idIntVal <= firstRec) && isPrevious ) ||
  180.             ( (idIntVal >= lastRec) &&  !isPrevious) ) {
  181.                 return false;
  182.             }
  183.  
  184.         // end of boundary checking...
  185.  
  186.         if((isAscending && !isPrevious) || (!isAscending && isPrevious) ){
  187.         // if the resultSet is already in ascending order and we want to goNext or
  188.         // if the resultSet is in descending order and we want to goPrevious,
  189.         // simply do a next() on the resultSet.
  190.             try {
  191.                 resultSet.next();
  192.             }
  193.             catch(SQLException e) {
  194.                 System.out.println("Exception from DataBase.goNext() : " +
  195.                                    e.getMessage());
  196.             }
  197.         }
  198.         else {
  199.             //if the resultSet is in descending order, produce a new resultSet
  200.             //in ascending order and do a order change..
  201.  
  202.             //re-make the resultSet in ascending or descending order
  203.             if(isAscending)
  204.                 search("SELECT * FROM customer ORDER by id DESC", false);
  205.             else
  206.                 search("SELECT * FROM customer ORDER BY id ASC", true);
  207.  
  208.             Integer currentVal = Integer.valueOf(id);
  209.  
  210.             try {
  211.  
  212.                 while(currentVal.intValue() != resultSet.getInt("id")){
  213.                     resultSet.next();
  214.                 }
  215.  
  216.                 resultSet.next();
  217.             }
  218.             catch(SQLException e) {
  219.                 System.out.println("Exception from DataBase.goNext() : " +
  220.                                   e.getMessage());
  221.             }
  222.         }
  223.         return true;
  224.     }
  225.  
  226.     boolean goNext(String id) {
  227.     // go to the next record w/ respect to customer id's in ascending order
  228.         return goNext(id, false);
  229.     }
  230.  
  231.     boolean goPrevious(String id) {
  232.     // go to the previous record w/ respect to customer id's in ascending order
  233.         return goNext(id, true);
  234.     }
  235.  
  236.    void showTable(TextField[] textFieldList, int columnList[]) {
  237.    // Show all records tied to textFields on our applet.
  238.         textFieldList[0].setEditable(false);
  239.         int n = textFieldList.length;
  240.         for(int i = 0; i < n; i++) {
  241.             try{
  242.                 textFieldList[i].setText(resultSet.getString(columnList[i]));
  243.             }
  244.             catch(SQLException e) {
  245.                 System.out.println("Exception from DataBase.showTextFields() : " +
  246.                                   e.getMessage());
  247.             }
  248.         }
  249.    }
  250.  
  251.    String currentID()
  252.    {
  253.    // this is just a little helper method to extract the current customer id on
  254.    // our current record.
  255.  
  256.         String returnStrg = new String("null");
  257.  
  258.         try {
  259.             returnStrg = new String(resultSet.getString("id"));
  260.         }
  261.         catch(SQLException e) {
  262.             System.out.println("Exception from DataBase.currentID() : " +
  263.                                   e.getMessage());
  264.         }
  265.         return returnStrg;
  266.  
  267.    }
  268.  
  269.    void clear(TextField[] textFieldList)
  270.    {
  271.    // clears all text in all the textFields.
  272.     int n = textFieldList.length;
  273.     String emptyString = new String();
  274.  
  275.         for(int i = 0; i < n; i++) {
  276.                 textFieldList[i].setText(emptyString);
  277.             }
  278.    }
  279.  
  280.    void goTo(String id)
  281.    {
  282.     //Goto the record indexed by id.
  283.     Integer currentVal = Integer.valueOf(id);
  284.  
  285.         try {
  286.             while(currentVal.intValue() != resultSet.getInt("id")){
  287.                 resultSet.next();
  288.             }
  289.         }
  290.         catch(SQLException e) {
  291.             System.out.println("Exception from DataBase.goTo() : " +
  292.                                e.getMessage());
  293.         }
  294.    }
  295. }
  296. public class customer extends Applet {
  297.  
  298.     public void init() {
  299.  
  300.          // Connection Initializtion.
  301.         dataBase = new
  302.             DataBase("jdbc:dbaw://localhost:8889/Watcom/SQL Anywhere 5.0 Sample/SQL Anywhere", "dba", "sql");
  303.  
  304.         dataBase.connectServer("symantec.itools.db.jdbc.Driver");
  305.  
  306.         super.init();
  307.  
  308.         //{{INIT_CONTROLS
  309.         setLayout(null);
  310.         resize(553,423);
  311.         titlePanel=new Panel();
  312.         titlePanel.setFont(new Font("TimesRoman",Font.BOLD,10));
  313.         titlePanel.setLayout(null);
  314.         add(titlePanel);
  315.         titlePanel.reshape(0,0,553,68);
  316.         Title=new Label("Customer Information Sheet");
  317.         Title.setFont(new Font("TimesRoman",Font.BOLD,36));
  318.         titlePanel.add(Title);
  319.         Title.reshape(49,15,455,45);
  320.         custIDLabel=new Label("Cust. ID:");
  321.         custIDLabel.setFont(new Font("Dialog",Font.BOLD,12));
  322.         add(custIDLabel);
  323.         custIDLabel.reshape(49,105,70,15);
  324.         custIDTextField=new TextField(6);
  325.         custIDTextField.setFont(new Font("Dialog",Font.BOLD,12));
  326.         add(custIDTextField);
  327.         custIDTextField.reshape(126,105,56,23);
  328.         fnameTextField=new TextField(12);
  329.         add(fnameTextField);
  330.         fnameTextField.reshape(126,135,105,23);
  331.         lnameTextField=new TextField(15);
  332.         add(lnameTextField);
  333.         lnameTextField.reshape(392,135,126,23);
  334.         fnameLabel=new Label("First Name:");
  335.         fnameLabel.setFont(new Font("Dialog",Font.BOLD,12));
  336.         add(fnameLabel);
  337.         fnameLabel.reshape(42,135,84,23);
  338.         lnameLabel=new Label("Last Name:");
  339.         lnameLabel.setFont(new Font("Dialog",Font.BOLD,12));
  340.         add(lnameLabel);
  341.         lnameLabel.reshape(301,135,77,23);
  342.         addressTextField=new TextField(47);
  343.         add(addressTextField);
  344.         addressTextField.reshape(126,180,392,23);
  345.         addressLabel=new Label("Address:");
  346.         addressLabel.setFont(new Font("Dialog",Font.BOLD,12));
  347.         add(addressLabel);
  348.         addressLabel.reshape(56,180,63,23);
  349.         cityLabel=new Label("City:");
  350.         cityLabel.setFont(new Font("Dialog",Font.BOLD,12));
  351.         add(cityLabel);
  352.         cityLabel.reshape(77,218,42,22);
  353.         cityTextField=new TextField(12);
  354.         add(cityTextField);
  355.         cityTextField.reshape(126,218,105,22);
  356.         stateLabel=new Label("State:");
  357.         stateLabel.setFont(new Font("Dialog",Font.BOLD,12));
  358.         add(stateLabel);
  359.         stateLabel.reshape(245,218,56,15);
  360.         stateTextField=new TextField(4);
  361.         add(stateTextField);
  362.         stateTextField.reshape(301,218,35,22);
  363.         zipLabel=new Label("ZIP:");
  364.         add(zipLabel);
  365.         zipLabel.reshape(371,218,42,22);
  366.         zipTextField=new TextField(11);
  367.         add(zipTextField);
  368.         zipTextField.reshape(420,218,98,22);
  369.         phoneLabel=new Label("Phone#:");
  370.         phoneLabel.setFont(new Font("Dialog",Font.BOLD,12));
  371.         add(phoneLabel);
  372.         phoneLabel.reshape(56,263,63,15);
  373.         phoneTextField=new TextField(12);
  374.         add(phoneTextField);
  375.         phoneTextField.reshape(126,263,105,22);
  376.         comLabel=new Label("Company:");
  377.         comLabel.setFont(new Font("Dialog",Font.BOLD,12));
  378.         add(comLabel);
  379.         comLabel.reshape(245,263,70,15);
  380.         comTextField=new TextField(24);
  381.         add(comTextField);
  382.         comTextField.reshape(315,263,203,22);
  383.         first=new Button("<<");
  384.         first.setFont(new Font("Dialog",Font.BOLD,12));
  385.         add(first);
  386.         first.reshape(77,308,35,30);
  387.         previous=new Button("<");
  388.         previous.setFont(new Font("Dialog",Font.BOLD,12));
  389.         add(previous);
  390.         previous.reshape(119,308,35,30);
  391.         next=new Button(">");
  392.         next.setFont(new Font("Dialog",Font.BOLD,12));
  393.         add(next);
  394.         next.reshape(161,308,35,30);
  395.         last=new Button(">>");
  396.         last.setFont(new Font("Dialog",Font.BOLD,12));
  397.         add(last);
  398.         last.reshape(203,308,35,30);
  399.         New=new Button("new");
  400.         New.setFont(new Font("Dialog",Font.BOLD,12));
  401.         add(New);
  402.         New.reshape(273,308,49,30);
  403.         add=new Button("add");
  404.         add.setFont(new Font("Dialog",Font.BOLD,12));
  405.         add(add);
  406.         add.reshape(329,308,49,30);
  407.         Update=new Button("update");
  408.         Update.setFont(new Font("Dialog",Font.BOLD,12));
  409.         add(Update);
  410.         Update.reshape(385,308,49,30);
  411.         searchChoiceBox= new Choice();
  412.         refresh=new Button("refresh");
  413.         refresh.setFont(new Font("Dialog",Font.BOLD,12));
  414.         add(refresh);
  415.         refresh.reshape(441,308,49,30);
  416.         add(searchChoiceBox);
  417.         searchChoiceBox.reshape(126,353,119,75);
  418.         searchChoiceBox.addItem("id");
  419.         searchChoiceBox.addItem("fname");
  420.         searchChoiceBox.addItem("lname");
  421.         searchChoiceBox.addItem("company_name");
  422.         searchChoiceBox.addItem("zip");
  423.         searchChoiceBox.addItem("state");
  424.         searchChoiceBox.addItem("phone");
  425.         searchLabel=new Label("Search By:");
  426.         searchLabel.setFont(new Font("Dialog",Font.BOLD,12));
  427.         add(searchLabel);
  428.         searchLabel.reshape(35,353,84,15);
  429.         searchTextField=new TextField(16);
  430.         add(searchTextField);
  431.         searchTextField.reshape(252,353,140,22);
  432.         search=new Button("search");
  433.         search.setFont(new Font("Dialog",Font.BOLD,12));
  434.         add(search);
  435.         search.reshape(399,353,49,22);
  436.         close=new Button("close");
  437.         close.setFont(new Font("Dialog",Font.BOLD,12));
  438.         add(close);
  439.         close.reshape(462,353,49,22);
  440.         infoTextField=new TextField(53);
  441.         add(infoTextField);
  442.         infoTextField.reshape(77,383,441,22);
  443.         //}}
  444.  
  445.         setBackground(Color.lightGray);
  446.         infoTextField.setEditable(false);
  447.  
  448.         // extract first and last record id's for boundary checking
  449.         dataBase.search("SELECT id FROM customer ORDER by id DESC", false);
  450.         try { dataBase.setLast(dataBase.resultSet.getString("id"));}
  451.         catch(SQLException e) { System.out.println(e.getMessage()); }
  452.  
  453.         // start off by generating all records in the customer table
  454.         dataBase.search("SELECT * FROM customer ORDER BY id ASC", true);
  455.  
  456.         try { dataBase.setFirst(dataBase.resultSet.getString("id")); }
  457.         catch(SQLException e) {System.out.println(e.getMessage()); }
  458.  
  459.         // one way to have global (w/ respect to only this class) variables
  460.         // with initializers.
  461.         TextField textFieldListLocal[] = {custIDTextField, fnameTextField, lnameTextField,
  462.                                           addressTextField, cityTextField, stateTextField,
  463.                                           zipTextField, phoneTextField, comTextField};
  464.         textFieldList = textFieldListLocal;
  465.  
  466.         int columnListLocal[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  467.         columnList = columnListLocal;
  468.  
  469.         // we want the id textField to be uneditable.
  470.         custIDTextField.setEditable(false);
  471.  
  472.         //disable add button until there is a need to use it.
  473.         add.disable();
  474.  
  475.         //Show the first record in our table.
  476.         dataBase.showTable(textFieldList, columnList);
  477.  
  478.     }
  479.  
  480.     public boolean handleEvent(Event event) {
  481.         if((event.target == next) && (event.id == Event.ACTION_EVENT)) {
  482.             ShowStatus("Processing Data Base on next request...");
  483.             if(dataBase.goNext(dataBase.currentID())) {
  484.                 dataBase.showTable(textFieldList, columnList);
  485.                 ShowStatus("");
  486.             }
  487.             else
  488.                 ShowStatus("You've reached a dead end!");
  489.  
  490.         }
  491.  
  492.         else if((event.target == previous) && (event.id == Event.ACTION_EVENT)) {
  493.             ShowStatus("Processing Data Base on previous request...");
  494.             if(dataBase.goPrevious(dataBase.currentID())) {
  495.                 dataBase.showTable(textFieldList, columnList);
  496.                 ShowStatus("");
  497.             }
  498.             else
  499.                 ShowStatus("You've reached a dead end!");
  500.         }
  501.  
  502.         else if((event.target == first) && (event.id == Event.ACTION_EVENT)) {
  503.             ShowStatus("Processing Data Base on first request...");
  504.             // generate a new resultSet in ascending order and show the first record
  505.             // of this resultSet
  506.             dataBase.search("SELECT * FROM customer ORDER BY id ASC", true);
  507.             dataBase.showTable(textFieldList, columnList);
  508.             ShowStatus("");
  509.         }
  510.  
  511.         else if((event.target == last) && (event.id == Event.ACTION_EVENT)) {
  512.             ShowStatus("Processing Data Base on last request...");
  513.             // generate a new resultSet in descending order and show the first record
  514.             // of this resultSet
  515.             dataBase.search("SELECT * FROM customer ORDER BY id DESC", false);
  516.             dataBase.showTable(textFieldList, columnList);
  517.             ShowStatus("");
  518.         }
  519.  
  520.         else if((event.target == New) && (event.id == Event.ACTION_EVENT)) {
  521.             custIDTextField.setEditable(true);
  522.             add.enable();
  523.             dataBase.clear(textFieldList);
  524.             ShowStatus("");
  525.         }
  526.  
  527.         else if((event.target == close) && (event.id == Event.ACTION_EVENT)) {
  528.             dataBase.closeServer();
  529.         }
  530.  
  531.         else if((event.target == add) && (event.id == Event.ACTION_EVENT)) {
  532.             ShowStatus("Attempting to add new record to Data Base...");
  533.             String comma = new String(" , ");
  534.             String quote = new String("'");
  535.  
  536.             // Some error checking: Illegal user input.
  537.             // if something is illegal, clear the textfield and return.
  538.             if(! errorHandling(textFieldList)) {
  539.                     return true;
  540.             }
  541.  
  542.             // Does the customer id already exist.
  543.             try {
  544.                 Statement stmt = dataBase.server.createStatement();
  545.                 ResultSet rs = stmt.executeQuery("select id from customer where id = " +
  546.                                                     custIDTextField.getText());
  547.                 rs.next();
  548.                 if(! rs.wasNull()) {
  549.                     ShowStatus("Cust. ID already exits, try another.");
  550.                     custIDTextField.setText("");
  551.                     return true;
  552.                 }
  553.                 stmt.close();
  554.             }
  555.             catch(SQLException e) {
  556.                 System.out.println("Exception caught during add : " + e.getMessage());
  557.             }
  558.  
  559.  
  560.  
  561.  
  562.             dataBase.search("select id from customer where id = " + custIDTextField.getText());
  563.             try {
  564.                 if(! dataBase.resultSet.wasNull()) {
  565.                     ShowStatus("Customer ID already exits, try another.");
  566.                     custIDTextField.setText("");
  567.                     return true;
  568.                 }
  569.             }
  570.             catch(SQLException e) {
  571.                 System.out.println("Exception caught during add : " + e.getMessage());
  572.             }
  573.  
  574.             String sqlStmt = new String ("insert into customer " + "( " + "id" + comma +
  575.                                         "fname" + comma + "lname" + comma +
  576.                                         "address" + comma + "city" + comma + "state" + comma+
  577.                                         "zip" + comma + "phone" + comma +
  578.                                         "company_name" + " ) " +
  579.                                         "values" + " ( " + custIDTextField.getText() + comma
  580.                                         + quote + fnameTextField.getText() + quote +
  581.                                         comma + quote + lnameTextField.getText() + quote +
  582.                                         comma + quote + addressTextField.getText() + quote +
  583.                                         comma + quote + cityTextField.getText() + quote +comma
  584.                                         + quote + stateTextField.getText() + quote + comma +
  585.                                         quote + zipTextField.getText() + quote + comma + quote +
  586.                                         phoneTextField.getText() + quote + comma + quote +
  587.                                         comTextField.getText() + quote + " ) " );
  588.  
  589.             dataBase.executeUpdate(sqlStmt);
  590.             add.disable();
  591.             // check for possibility of a new firstRec or lastRec
  592.             String id = new String(custIDTextField.getText());
  593.             Integer idInteger = null;
  594.             try {
  595.                 idInteger = new Integer(id);
  596.             }
  597.             catch(NumberFormatException e) {
  598.                 System.out.println("Exception caught in add() : " + e.getMessage());
  599.             }
  600.  
  601.             if(idInteger.intValue() < dataBase.firstRec)
  602.                 dataBase.setFirst(id);
  603.             if(idInteger.intValue() > dataBase.lastRec)
  604.                 dataBase.setLast(id);
  605.  
  606.             custIDTextField.setEditable(false);
  607.             dataBase.search("SELECT * FROM customer WHERE id >= " +
  608.                                     custIDTextField.getText() + " ORDER by id ASC", true);
  609.             ShowStatus("");
  610.         }
  611.  
  612.         else if((event.target == Update) && (event.id == Event.ACTION_EVENT)) {
  613.             ShowStatus("Attempting to update record #" + dataBase.currentID() +
  614.                         " from Data Base ...");
  615.             String quote = new String("'");
  616.             String comma = new String(" , ");
  617.             String sqlStmt = new String("UPDATE customer SET fname = " +
  618.                                         quote + fnameTextField.getText() +
  619.                                         quote + comma + "lname = " + quote +
  620.                                         lnameTextField.getText() + quote + comma +
  621.                                         "address = " + quote +
  622.                                         addressTextField.getText() + quote +
  623.                                         comma + "state = " + quote + stateTextField.getText() +
  624.                                         quote + comma + "city = " + quote + cityTextField.getText()
  625.                                         + quote + comma +  "zip = " + quote +
  626.                                         zipTextField.getText() + quote + comma +
  627.                                         "phone = " +  quote + phoneTextField.getText() + quote
  628.                                         + comma + "company_name = " + quote +
  629.                                         comTextField.getText() + quote +
  630.                                         " WHERE id = " + dataBase.currentID());
  631.  
  632.             dataBase.executeUpdate(sqlStmt);
  633.             add.disable();
  634.             dataBase.search("SELECT * FROM customer WHERE id >= " +
  635.                                     Integer.valueOf(custIDTextField.getText())
  636.                                     + " ORDER by id ASC", true);
  637.             ShowStatus("");
  638.  
  639.         }
  640.  
  641.         else if((event.target == refresh) && (event.id == Event.ACTION_EVENT)) {
  642.             // refresh dataBase, especially afer searches
  643.             ShowStatus("Refreshing Data Base...");
  644.             String sqlStmt = new String("select * from customer order by id asc");
  645.             dataBase.search(sqlStmt, true);
  646.             dataBase.showTable(textFieldList, columnList);
  647.             ShowStatus("");
  648.         }
  649.  
  650.         else if((event.target == search) && (event.id == Event.ACTION_EVENT)) {
  651.             String key = searchTextField.getText();
  652.             String sqlStmt = null;
  653.             String countSql = null;
  654.             String searchItem = searchChoiceBox.getSelectedItem();
  655.  
  656.             ShowStatus("Searching for key: " + key + " from Data Base...");
  657.             if(key.equals("")) {
  658.                 //if the textfield contains nothing, do nothing.
  659.                 return true;
  660.             }
  661.             else if(! searchItem.equals("id")) {
  662.                 // if search by other than Customer ID
  663.                 String quote = new String("'");
  664.                 countSql = "SELECT COUNT (id) FROM customer WHERE "
  665.                                   + searchItem + " = " +
  666.                                   quote + key + quote;
  667.                 sqlStmt = new String("SELECT * FROM customer WHERE " +
  668.                                     searchItem + " = " +
  669.                                     quote + key + quote + "ORDER BY id ASC");
  670.  
  671.             }
  672.             else {  // must be performing a search with respect to customer ID.
  673.                  // Some error checking: Illegal user input.
  674.                 if (!isNumeric(key)) {
  675.                     ShowStatus("customer ID must be a number!!");
  676.                     searchTextField.setText("");
  677.                     return true;
  678.                 }
  679.                 countSql = "SELECT COUNT (id) FROM customer WHERE id = "
  680.                             + key;
  681.                 sqlStmt = new String("SELECT * FROM customer WHERE id >= "
  682.                                     + key + " ORDER BY id ASC");
  683.             }
  684.             String currentID = new String(dataBase.currentID());
  685.             int countRecs = 0;
  686.             try {
  687.                     Statement stmt = dataBase.server.createStatement();
  688.                     ResultSet rs = stmt.executeQuery(countSql);
  689.                     rs.next();
  690.                     countRecs = rs.getInt(1);
  691.                     stmt.close();
  692.             }
  693.             catch(SQLException e) {
  694.                 System.out.println("Exception caught in search : " + e.getMessage());
  695.             }
  696.  
  697.             if(countRecs == 0) {
  698.                 ShowStatus("No records retrieved from key: " + key);
  699.                 return true;
  700.             }
  701.             ShowStatus(countRecs + " record(s) retrieved from key : " + key);
  702.             dataBase.search(sqlStmt, true);
  703.             dataBase.showTable(textFieldList, columnList);
  704.  
  705.         }
  706.         else if (event.id == Event.WINDOW_DESTROY) {
  707.             dataBase.closeServer();
  708.             System.exit(0);
  709.         }
  710.         showDefaultStatus();
  711.         return super.handleEvent(event);
  712.      }
  713.  
  714.  
  715.     // method to check if a string is numeric.
  716.     boolean isNumeric(String number) {
  717.  
  718.         try {
  719.             Integer val = new Integer(number);
  720.             return true;
  721.         }
  722.         catch(NumberFormatException e) {
  723.             System.out.println("Exception from isNumeric() : " + e.getMessage());
  724.             return false;
  725.         }
  726.     }
  727.  
  728.     boolean errorHandling(TextField[] textFieldList) {
  729.  
  730.          if (!isNumeric(custIDTextField.getText())) {
  731.                 ShowStatus("customer ID must be a number!!");
  732.                 custIDTextField.setText("");
  733.                 return false;
  734.          }
  735.         return true;
  736.     }
  737.  
  738.     void ShowStatus(String s) {
  739.         infoTextField.setText(s);
  740.     }
  741.  
  742.     void showDefaultStatus() {
  743.         getAppletContext().showStatus("Symantec Corp., 1996");
  744.     }
  745.  
  746.     //Tabbing stuff... from Ankur
  747.  
  748.     public void start() {
  749.         custIDTextField.requestFocus();
  750.     }
  751.  
  752.     public boolean keyDown(Event evt, int key) {
  753.         if ((key==9) || (key==10)) {
  754.             tab(evt);
  755.             return true;
  756.         }
  757.         else return super.keyDown(evt, key);
  758.     }
  759.  
  760.     void tab(Event evt) {
  761.         Component temp = (Component)evt.target;
  762.         boolean shiftpressed=evt.shiftDown();
  763.  
  764.         int index = tabindex(temp, shiftpressed);
  765.         Component newtab = temp.getParent().getComponent(index);
  766.         newtab.requestFocus();
  767.         if (newtab instanceof TextField)
  768.             ((TextField)newtab).selectAll();
  769.     }
  770.  
  771.     int tabindex(Component current, boolean shiftpressed) {  //Returns the tab index of the next component
  772.         Component[] temp;
  773.         temp = current.getParent().getComponents();
  774.         int increment;
  775.         boolean foundit=false;
  776.  
  777.         if (!(shiftpressed))
  778.         {
  779.             increment=1;
  780.             for (int i=0;i<(temp.length)-1;i++)
  781.             {
  782.              if (current == temp[i]) foundit=true;
  783.              if (foundit)
  784.              {
  785.                  Component next=temp[i+increment];
  786.                  if ((next instanceof TextComponent) || (next instanceof Choice) || (next instanceof Button) || (next instanceof List))
  787.                       return (i+increment);
  788.                  else if (next instanceof Checkbox)
  789.                       {
  790.                         Checkbox tempcheckbox=(Checkbox)next;
  791.                        if (tempcheckbox.getCheckboxGroup()==null) return (i+increment);
  792.                       }
  793.               }
  794.             }
  795.         }
  796.         else
  797.         {
  798.             increment=-1;
  799.             for (int i=(temp.length)-1;i>=1;i--) {
  800.              if (current == temp[i]) foundit=true;
  801.              if (foundit)
  802.               {
  803.                  Component next=temp[i+increment];
  804.                  if ((next instanceof TextComponent) || (next instanceof Choice) || (next instanceof Button) || (next instanceof List))
  805.                       return (i+increment);
  806.                  else if (next instanceof Checkbox)
  807.                       {
  808.                         Checkbox tempcheckbox=(Checkbox)next;
  809.                        if (tempcheckbox.getCheckboxGroup()==null) return (i+increment);
  810.                       }
  811.               }
  812.             }
  813.         }
  814.         if (shiftpressed) {return (temp.length)-1;} else return 0;  //Base case or if we've hit the final component
  815. }
  816.     // end of tabbing stuff.
  817.  
  818.  
  819.     //{{DECLARE_CONTROLS
  820.     Panel titlePanel;
  821.     Label Title;
  822.     TextField fnameTextField;
  823.     TextField lnameTextField;
  824.     Label fnameLabel;
  825.     Label lnameLabel;
  826.     TextField addressTextField;
  827.     Label addressLabel;
  828.     Label cityLabel;
  829.     TextField cityTextField;
  830.     Label stateLabel;
  831.     TextField stateTextField;
  832.     Label zipLabel;
  833.     TextField zipTextField;
  834.     Label phoneLabel;
  835.     TextField phoneTextField;
  836.     Label comLabel;
  837.     TextField comTextField;
  838.     Button first;
  839.     Button previous;
  840.     Button next;
  841.     Button last;
  842.     Button New;
  843.     Button add;
  844.     Button Update;
  845.     Choice searchChoiceBox;
  846.     Label searchLabel;
  847.     TextField searchTextField;
  848.     Button close;
  849.     Label custIDLabel;
  850.     TextField custIDTextField;
  851.     Button search;
  852.     Button refresh;
  853.     TextField infoTextField;
  854.     //}}
  855.  
  856.     DataBase dataBase;
  857.     TextField textFieldList[];
  858.     int columnList[];
  859.  
  860.     }
  861.