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 / INVENTORIG.JAVA next >
Encoding:
Java Source  |  1997-02-21  |  28.0 KB  |  802 lines

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