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 / Inventory / INVENTORY.JAVA < prev    next >
Encoding:
Java Source  |  1997-02-21  |  31.8 KB  |  863 lines

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