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 / PhoneBook / PHONEBASEAPPJDBCVERSION1.JAVA < prev    next >
Encoding:
Java Source  |  1997-02-21  |  21.4 KB  |  583 lines

  1. /*
  2.  * PhoneBaseAppJDBC.java
  3.  * Version 1.0
  4.  *
  5.  * Written by Long Huynh    7.30.96
  6.  *
  7.  * Copyright (c) 1996 Symantec.  All Rights Reserved.
  8.  *
  9.  */
  10.  
  11. import java.awt.*;
  12. import java.util.Properties;
  13. import java.lang.Thread;
  14. import java.sql.*;
  15. import com.symantec.tools.tsunami.scale.SCLSession;
  16. import com.symantec.tools.tsunami.scale.SCLRelView;
  17.  
  18. public class PhoneBaseAppJDBC extends java.applet.Applet implements Runnable{
  19.  
  20.     public void init() {
  21.  
  22.         // Initialize Background and Text Color
  23.         this.setBackground(Color.white);
  24.         this.setForeground(Color.red);
  25.  
  26.         super.init();
  27.  
  28.         // Initialize Properties
  29.         props.put("user","dba");
  30.         props.put("password","sql");
  31.  
  32.         // Initialize TextFields
  33.         firstfield = new TextField();
  34.         firstfield.setBackground(current_color);
  35.  
  36.         lastfield = new TextField();
  37.         lastfield.setBackground(current_color);
  38.  
  39.         passfield = new TextField();
  40.         passfield.setBackground(current_color);
  41.         passfield.disable();
  42.  
  43.         waitfield = new TextField();
  44.         waitfield.setBackground(current_color);
  45.         waitfield.disable();
  46.         waitfield.setText("Ready.");
  47.  
  48.         // Initialize TextArea
  49.         output = new TextArea();
  50.         output.setEditable(false);
  51.  
  52.         // Initialize Choices
  53.         deptchoice = new Choice();
  54.         deptchoice.addItem("All");
  55.         deptchoice.select(0);
  56.         deptchoice.disable();
  57.  
  58.         sitechoice = new Choice();
  59.         sitechoice.addItem("All");
  60.         sitechoice.select(0);
  61.         sitechoice.disable();
  62.  
  63.         esnchoice = new Choice();
  64.         esnchoice.addItem("All");
  65.         esnchoice.select(0);
  66.         esnchoice.disable();
  67.  
  68.         // Initialize CheckBoxGroup
  69.         options = new CheckboxGroup();
  70.         quick_search = new Checkbox("Search Names Only (Quick Search)");
  71.         quick_search.setState(true);
  72.  
  73.         // Initialize Buttons
  74.         searchbutton = new Button("Search for Extension");
  75.         clearbutton = new Button("Clear Form");
  76.  
  77.         // Initialize Labels
  78.         firstlabel = new Label("First Name:");
  79.         lastlabel = new Label("Last Name:");
  80.         deptlabel = new Label("Department:");
  81.         sitelabel = new Label("City:");
  82.         esnlabel = new Label("Zip Code:");
  83.         statuslabel = new Label("Status:");
  84.         messagelabel = new Label("Feedback:");
  85.  
  86.         //{{INIT_CONTROLS
  87.         setLayout(null);
  88.  
  89.         // Add CheckboxGroup
  90.         add(quick_search);
  91.         quick_search.reshape(insets().left+15, insets().top+5, 250, 20);
  92.  
  93.         // Add First Name
  94.         add(firstlabel);
  95.         firstlabel.reshape(insets().left+2, insets().top+30, 80, 20);
  96.         add(firstfield);
  97.         firstfield.reshape(insets().left+85, insets().top+30, 160, 20);
  98.         firstfield.requestFocus();
  99.  
  100.         // Add Last Name
  101.         add(lastlabel);
  102.         lastlabel.reshape(insets().left+250, insets().top+30, 80, 20);
  103.         add(lastfield);
  104.         lastfield.reshape(insets().left+335, insets().top+30, 160, 20);
  105.  
  106.         // Add Department
  107.         add(deptlabel);
  108.         deptlabel.reshape(insets().left+2, insets().top+55, 80, 20);
  109.         add(deptchoice);
  110.         deptchoice.reshape(insets().left+85, insets().top+55, 160, 20);
  111.  
  112.         // Add Site
  113.         add(sitelabel);
  114.         sitelabel.reshape(insets().left+250, insets().top+55, 80, 20);
  115.         add(sitechoice);
  116.         sitechoice.reshape(insets().left+335, insets().top+55, 160, 20);
  117.  
  118.         // Add ESN
  119.         add(esnlabel);
  120.         esnlabel.reshape(insets().left+2, insets().top+80, 80, 20);
  121.         add(esnchoice);
  122.         esnchoice.reshape(insets().left+85, insets().top+80, 160, 20);
  123.  
  124.         // Add Search for Extension Button
  125.         add(searchbutton);
  126.         searchbutton.reshape(insets().left+260, insets().top+80, 140, 20);
  127.  
  128.         // Add Clear Button
  129.         add(clearbutton);
  130.         clearbutton.reshape(insets().left+405, insets().top+80, 90, 20);
  131.  
  132.         // Add Waitfield
  133.         add(statuslabel);
  134.         statuslabel.reshape(insets().left+2, insets().top+105, 80, 20);
  135.         add(waitfield);
  136.         waitfield.reshape(insets().left+85, insets().top+105, 415, 20);
  137.  
  138.         // Add Passfield
  139.         add(messagelabel);
  140.         messagelabel.reshape(insets().left+2, insets().top+130, 80, 20);
  141.         add(passfield);
  142.         passfield.reshape(insets().left+85, insets().top+130, 415, 20);
  143.  
  144.         // Add Output
  145.         add(output);
  146.         output.reshape(insets().left+15, insets().top+155, 485, 250);
  147.  
  148.         // Show opening messages
  149.         opening();
  150.  
  151.         show();
  152.         //}}
  153.     }
  154.  
  155.     // add_choices()
  156.     private void add_choices() {
  157.         try {
  158.             Statement stmt = server.createStatement();
  159.             ResultSet rs_temp;
  160.  
  161.             try {
  162.                 waitfield.setText("Please wait while PhoneBase is loading...");
  163.                 passfield.setText("Opening database '" + db_name + "'");
  164.                 rs_temp = stmt.executeQuery(
  165.                 //SCLRelView dept = Tsunamiserver.createView(db, "dept",
  166.                     "SELECT DISTINCT " + dept_SQL + " FROM " + db_name + " ORDER BY " + dept_SQL
  167.                     );
  168.                 //    ,0, 200, true, true, props);
  169.                 passfield.setText("Adding department choices...");
  170.                 rs_temp.next();
  171.                 while (rs_temp.getString(1) == null) rs_temp.next();
  172.                 deptchoice.addItem(rs_temp.getString(1));
  173.                 while (rs_temp.next()) { deptchoice.addItem(rs_temp.getString(1)); }
  174.                 deptchoice.enable();
  175.             }
  176.             catch (SQLException open_error) {
  177.                 passfield.setText("Could not load department choices!");
  178.                 sleep();
  179.             }
  180.         }
  181.         catch (SQLException e) {
  182.             passfield.setText("Unable to create statemenr for deptchoice!");
  183.             sleep();
  184.         }
  185.  
  186.         try {
  187.             Statement stmt = server.createStatement();
  188.             ResultSet rs_temp;
  189.  
  190.             try {
  191.                 passfield.setText("Adding site choices...");
  192.                 rs_temp = stmt.executeQuery(
  193.                 //SCLRelView site = Tsunamiserver.createView(db, "site",
  194.                     "SELECT DISTINCT " + site_SQL + " FROM " + db_name + " ORDER BY " + site_SQL
  195.                     );
  196.                 //    ,0, 50, true, true, props);
  197.                 rs_temp.next();
  198.                 while (rs_temp.getString(1) == null) rs_temp.next();
  199.                 sitechoice.addItem(rs_temp.getString(1));
  200.                 while (rs_temp.next()) { sitechoice.addItem(rs_temp.getString(1)); }
  201.                 sitechoice.enable();
  202.             }
  203.             catch (SQLException open_error) {
  204.                 passfield.setText("Could not load site choices!");
  205.                 sleep();
  206.             }
  207.         }
  208.         catch (SQLException e) {
  209.             passfield.setText("Unable to create statemenr for sitechoice!");
  210.             sleep();
  211.         }
  212.  
  213.         try {
  214.             Statement stmt = server.createStatement();
  215.             ResultSet rs_temp;
  216.  
  217.             try {
  218.                 passfield.setText("Adding esn choice...");
  219.                 rs_temp = stmt.executeQuery(
  220.                 //SCLRelView esn = Tsunamiserver.createView(db, "esn",
  221.                     "SELECT DISTINCT " + esn_SQL + " FROM " + db_name + " ORDER BY " + esn_SQL
  222.                     );
  223.                 //    ,0, 50, true, true, props);
  224.                 rs_temp.next();
  225.                 while (rs_temp.getString(1) == null) rs_temp.next();
  226.                 esnchoice.addItem(rs_temp.getString(1));
  227.                 while (rs_temp.next()) { esnchoice.addItem(rs_temp.getString(1)); }
  228.                 esnchoice.enable();
  229.             }
  230.             catch (SQLException open_error) {
  231.                 passfield.setText("Could not load esn choices!");
  232.                 sleep();
  233.             }
  234.  
  235.             waitfield.setText("Ready.");
  236.         }
  237.         catch (SQLException e) {
  238.             passfield.setText("Unable to create statemenr for sitechoice!");
  239.             sleep();
  240.         }
  241.     }
  242.  
  243.     // close_server()
  244.     private void close_server() {
  245.         passfield.setText("Closing server " + server_url + "...");
  246.         try {
  247.             server.close();
  248.             passfield.setText("Closing server " + server_url + "... Connection closed!");
  249.             good_connection = false;
  250.         }
  251.         catch (Exception close_error) {
  252.             passfield.setText("Could not close " + server_url);
  253.             sleep();
  254.         }
  255.     }
  256.  
  257.     // connect_server()
  258.     private void connect_server() {
  259.         passfield.setText("Opening server " + server_url + "...");
  260.         try {
  261.             tsunami_driver = (Driver)Class.forName(tsunami_driver_name).newInstance();
  262.             passfield.setText("Opening server " + server_url + "... Connection successful!");
  263.             dmanager.registerDriver(tsunami_driver);
  264.             server = dmanager.getConnection(server_url, props);
  265.             good_connection = true;
  266.         }
  267.         catch (SQLException open_error) {
  268.             passfield.setText("Could not open server " + server_url);
  269.             sleep();
  270.         }
  271.         catch (InstantiationException e) {
  272.             passfield.setText(e.getMessage());
  273.             sleep();
  274.         }
  275.         catch (ClassNotFoundException e) {
  276.             passfield.setText(e.getMessage());
  277.             sleep();
  278.         }
  279.         catch (IllegalAccessException e) {
  280.             passfield.setText(e.getMessage());
  281.             sleep();
  282.         }
  283.     }
  284.  
  285.     // generate_SQL()
  286.     private String generate_SQL() {
  287.         waitfield.setText("Search initiated...");
  288.         String from = new String("FROM " + db_name + " ");
  289.         boolean first_exist = false, last_exist = false, not_first_one = false;
  290.         boolean dept_exist = false, site_exist = false, esn_exist = false;
  291.         int wild_return = has_wildcard();
  292.  
  293.         if (firstfield.getText().length() != 0) first_exist = true;
  294.         if (lastfield.getText().length() != 0) last_exist = true;
  295.         if (deptchoice.getSelectedIndex() != 0) dept_exist = true;
  296.         if (sitechoice.getSelectedIndex() != 0) site_exist = true;
  297.         if (esnchoice.getSelectedIndex() != 0) esn_exist = true;
  298.  
  299.         if (firstfield.getText().compareTo("*") == 0) first_exist = false;
  300.         if (lastfield.getText().compareTo("*") == 0) last_exist = false;
  301.  
  302.         if (first_exist || last_exist || dept_exist || site_exist || esn_exist) {
  303.             from = from.concat("WHERE");
  304.  
  305.             if ( (first_exist) && ((wild_return == 0) || (wild_return == 2)) ){
  306.                 not_first_one = true;
  307.                 from = from.concat(" " + first_SQL + " = '" + firstfield.getText() + "'");
  308.             }
  309.             else if ( (first_exist) && ((wild_return == 1) || (wild_return == 3)) ) {
  310.                 not_first_one = true;
  311.                 from = from.concat(" " + first_SQL + " LIKE '"
  312.                     + (firstfield.getText()).replace('*', '%').replace('?', '_') + "'");
  313.             }
  314.  
  315.             if ( (last_exist) && ((wild_return == 0) || (wild_return == 1)) ) {
  316.                 if (not_first_one) { from = from.concat(" AND"); }
  317.                 from = from.concat(" " + last_SQL + " = '" + lastfield.getText() + "'");
  318.             }
  319.             else if ( (last_exist) && ((wild_return == 2) || (wild_return == 3)) ) {
  320.                 if (not_first_one) { from = from.concat(" AND"); }
  321.                 not_first_one = true;
  322.                 from = from.concat(" " + last_SQL + " LIKE '"
  323.                     + (lastfield.getText()).replace('*', '%').replace('?', '_') + "'");
  324.             }
  325.  
  326.             if (dept_exist) {
  327.                 if (not_first_one) { from = from.concat(" AND"); }
  328.                 from = from.concat(" " + dept_SQL + " = '" + deptchoice.getSelectedItem() + "'");
  329.             }
  330.             if (site_exist) {
  331.                 if (not_first_one) { from = from.concat(" AND"); }
  332.                 from = from.concat(" " + site_SQL + " = '" + sitechoice.getSelectedItem() + "'");
  333.             }
  334.             if (esn_exist) {
  335.                 if (not_first_one) { from = from.concat(" AND"); }
  336.                 from = from.concat(" " + esn_SQL + " = '" + esnchoice.getSelectedItem() + "'");
  337.             }
  338.         }
  339.         return (select + from);
  340.     }
  341.  
  342.     // db_search()
  343.     private void db_search() {
  344.         int counter = 0;
  345.  
  346.         // Show Status
  347.         waitfield.setText("Please wait while PhoneBase is loading...");
  348.  
  349.         // Print the results
  350.         String searchfirst = new String(firstfield.getText());
  351.         String searchlast = new String(lastfield.getText());
  352.  
  353.         output.setText("              PhoneBase Search Result\n");
  354.         output.appendText("====================================\n");
  355.  
  356.         try {
  357.             Statement stmt = server.createStatement();
  358.             ResultSet rs_temp;
  359.  
  360.             try {
  361.                 passfield.setText("Opening database '" + db_name + "'");
  362.                 rs_temp = stmt.executeQuery(
  363.                 //SCLRelView sr = Tsunamiserver.createView(db, "sr",
  364.                     generate_SQL());
  365.                 //    ,0, 50, true, true, props);
  366.  
  367.                 rs_temp.next();
  368.                 output.appendText("Located: " + rs_temp.getString(2) + ", " + rs_temp.getString(1)
  369.                     + " at extension " + rs_temp.getString(3) + "\n");
  370.                 counter++;
  371.  
  372.                 while(rs_temp.next()) {
  373.                     output.appendText("Located: " + rs_temp.getString(2) + ", "
  374.                         + rs_temp.getString(1) + " at extension " + rs_temp.getString(3) + "\n");
  375.                     counter++;
  376.                 }
  377.                 output.appendText("\n" + counter + " hit(s) found.\n");
  378.             }
  379.             catch (Exception filter_error) {
  380.                 passfield.setText("Could not continue with search algorithm!");
  381.             }
  382.         }
  383.         catch (Exception filter_error) {
  384.             passfield.setText("Could not continue with search algorithm!");
  385.         }
  386.  
  387.         if (counter == 0) output.appendText("\nNo hits found.\n\n");
  388.  
  389.         output.appendText("====================================\n");
  390.  
  391.         close_server();
  392.  
  393.         waitfield.setText("Ready.");
  394.     }
  395.  
  396.     // has_wildcard()
  397.     private int has_wildcard() {
  398.         int num_of_wildcard = 0;
  399.  
  400.         if ((firstfield.getText()).indexOf("*", 0) != -1) { num_of_wildcard = 1; }
  401.         else if ((firstfield.getText()).indexOf("?", 0) != -1) { num_of_wildcard = 1; }
  402.         if ((lastfield.getText()).indexOf("*", 0) != -1) {
  403.             if (num_of_wildcard == 1) num_of_wildcard = 3;
  404.             else num_of_wildcard = 2;
  405.         }
  406.         else if ((lastfield.getText()).indexOf("?", 0) != -1) {
  407.             if (num_of_wildcard == 1) num_of_wildcard = 3;
  408.             else num_of_wildcard = 2;
  409.         }
  410.         return num_of_wildcard;
  411.     }
  412.  
  413.     // is_empty()
  414.     private boolean is_empty() {
  415.         if (((firstfield.getText()).length() == 0) && ((lastfield.getText()).length() == 0))
  416.             return true;
  417.         else return false;
  418.     }
  419.  
  420.     // opening()
  421.     private void opening() {
  422.         output.setText("Welcome to PhoneBaseJDBC '96 Build 09\n");
  423.         output.appendText("==============================================\n");
  424.         output.appendText("In light of not being able to connect to Excel,\n");
  425.         output.appendText("this search program will use Watcom's Sample\n");
  426.         output.appendText("Changes include:\n");
  427.         output.appendText("    - Employee's Dept_id instead of Department (Name)\n");
  428.         output.appendText("    - Employee's Residential City instead of Work Site\n");
  429.         output.appendText("    - Employee's ZipCode instead of Work ESN\n\n");
  430.         output.appendText("(Changes are external only. Internally, there may still)\n");
  431.         output.appendText("(be labels called esn and site.                               )\n\n");
  432.         output.appendText("This program, however, still retains:\n");
  433.         output.appendText("    - Conditional searches (where clause).\n");
  434.         output.appendText("    - Supports wildcard searches via '*'.\n");
  435.         output.appendText("    - Supports single-place wildcard searches via '?'.\n");
  436.         output.appendText("==============================================\n");
  437.     }
  438.  
  439.     // run()
  440.     public void run() {
  441.         int cntr = 0;
  442.  
  443.         while (kicker != null) {
  444.             cntr++;
  445.             if ((cntr%1000) == 0) first = true;
  446.             try {kicker.sleep(25);} catch (InterruptedException e) {}
  447.         }
  448.     }
  449.  
  450.     // sleep();
  451.     private void sleep() {
  452.         close_server();
  453.         waitfield.setText("Exception caught! Please notify maintenance of exception below.");
  454.         firstfield.disable();
  455.         this.disable();
  456.       //  for (int i = 0; i < 3; i++) i = 1;
  457.     }
  458.  
  459.     // start()
  460.     public void start() {
  461.         if (kicker == null) {
  462.             kicker = new Thread(this);
  463.             kicker.setPriority(kicker.MIN_PRIORITY);
  464.             kicker.start();
  465.         }
  466.     }
  467.  
  468.     // stop()
  469.     public void stop() {
  470.         kicker = null;
  471.         super.stop();
  472.     }
  473.  
  474.     // handleEvent()
  475.     public boolean handleEvent(Event event) {
  476.         Object evt = event.target;
  477.  
  478.         if (event.id == Event.KEY_PRESS) {
  479.             if (event.key == 9) {
  480.                 if (evt == firstfield) {lastfield.requestFocus();}
  481.                 else if (evt == lastfield) {deptchoice.requestFocus();}
  482.                 else if (evt == deptchoice) {sitechoice.requestFocus();}
  483.                 else if (evt == sitechoice) {esnchoice.requestFocus();}
  484.                 else if (evt == esnchoice) {searchbutton.requestFocus();}
  485.                 else if (evt == searchbutton) {clearbutton.requestFocus();}
  486.                 else if (evt == clearbutton) {passfield.requestFocus();}
  487.                 else if (evt == passfield) {firstfield.requestFocus();}
  488.                 else if (evt == output) {firstfield.requestFocus(); }
  489.                 else { InterruptedException e; return false;}
  490.             }
  491.             else if ((event.key == 10) && (!is_empty())) {
  492.                 start();
  493.                 searchbutton.disable();
  494.                 connect_server();
  495.                 if (good_connection) { db_search(); }
  496.                 searchbutton.enable();
  497.             }
  498.             else return super.handleEvent(event);
  499.             return true;
  500.         }
  501.         else if ((event.id == Event.ACTION_EVENT) && (evt instanceof Button)) {
  502.             if (evt == clearbutton) {
  503.                 firstfield.setText("");
  504.                 lastfield.setText("");
  505.                 deptchoice.select(0);
  506.                 sitechoice.select(0);
  507.                 esnchoice.select(0);
  508.                 firstfield.requestFocus();
  509.             }
  510.             else if ((evt == searchbutton) && (!is_empty())) {
  511.                 start();
  512.                 searchbutton.disable();
  513.                 connect_server();
  514.                 if (good_connection) { db_search(); }
  515.                 searchbutton.enable();
  516.             }
  517.             else { InterruptedException e; return false; }
  518.             return true;
  519.         }
  520.         else if ((event.id == Event.ACTION_EVENT) && (evt instanceof Checkbox)) {
  521.             if ((evt == quick_search) && (quick_search.getState() == false)) {
  522.                 quick_search.setState(false);
  523.                 searchbutton.disable();
  524.  
  525.                 connect_server();
  526.                 if (good_connection) {
  527.                     add_choices();
  528.                     close_server();
  529.                 }
  530.  
  531.                 searchbutton.enable();
  532.             }
  533.             if ((evt == quick_search) && (quick_search.getState() == true)) {
  534.                 quick_search.setState(true);
  535.                 searchbutton.disable();
  536.  
  537.                 deptchoice.select(0);
  538.                 deptchoice.disable();
  539.                 sitechoice.select(0);
  540.                 sitechoice.disable();
  541.                 esnchoice.select(0);
  542.                 esnchoice.disable();
  543.  
  544.                 searchbutton.enable();
  545.             }
  546.             else { InterruptedException e; return false; }
  547.             return true;
  548.         }
  549.         else if (event.id == Event.WINDOW_DESTROY) { System.exit(0); return true; }
  550.         else return super.handleEvent(event);
  551.     }
  552.  
  553.     //{{DECLARE_CONTROLS
  554.     private boolean first = false, good_connection = false;
  555.     private Color current_color = Color.white;
  556.     private String tsunami_driver_name = "com.symantec.tools.tsunami.jdbc.Driver";
  557.     private String server_url = "jdbc:scale://localhost:8889/Watcom/Sample/Sample";
  558.     //private String server_url = "jdbc:scale://155.64.37.27:8889/Watcom/Sample/Sample";
  559.     private String db = "WATCOM/Sample/Sample";
  560.     private String db_name = "employee";
  561.     private String first_SQL = "emp_fname";
  562.     private String last_SQL = "emp_lname";
  563.     private String dept_SQL = "dept_id";
  564.     private String site_SQL = "city";
  565.     private String esn_SQL = "zip_code";
  566.     private String select = new String("SELECT emp_fname, emp_lname, phone ");
  567.  
  568.     Thread kicker = null;
  569.     TextField firstfield, lastfield, passfield, waitfield;
  570.     TextArea output;
  571.     Choice deptchoice, sitechoice, esnchoice;
  572.     CheckboxGroup options;
  573.     Checkbox quick_search;
  574.     Button searchbutton, clearbutton;
  575.     Label firstlabel, lastlabel, deptlabel, sitelabel, esnlabel, statuslabel, messagelabel;
  576.  
  577.     Driver tsunami_driver;
  578.     DriverManager dmanager;
  579.     Connection server;
  580.     SCLSession Tsunamiserver;
  581.     Properties props = new Properties();
  582. }
  583.