home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 27.3 KB | 708 lines |
- /**************************************************************************************************
- *
- * Class name: PhoneBaseAppJDBC
- * Purpose: This application accesses the employee table and demonstrates basic database
- * navigation and functionality (e.g., Add Save, First, Next)
- *
- * Imports: java.applet.Applet;
- * java.util.Properties;
- * java.lang.Thread;
- * java.lang.String;
- * java.awt.*
- * symjava.sql.*
- * java.io.*
- *
- * Methods include: public boolean handleEvent(Event event)
- * public void init()
- * public void paint(Graphics g)
- * public void stoplight()
- *
- *
- * Additional Notes:
- * THIS SOFTWARE HAS BEEN COMPILED AND EXECUTED SUCCESSFULLY IN SPECIFIC SYMANTEC
- * ENVIRONMENTS, AND IS BEING PROVIDED ONLY AS SAMPLE CODE.
- *
- * SYMANTEC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SYMANTEC SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * Please see the Sample Code Configuration item in the Read Me file for additional
- * information about the sample database used in this applet.
- *
- * Copyright (c) 1996 Symantec. All Rights Reserved.
- *
- ***************************************************************************************************/
-
- import java.applet.Applet;
- import java.awt.*;
- import java.util.Properties;
- import java.lang.Thread;
- import java.lang.String;
- import symjava.sql.*;
-
- public class PhoneBaseAppJDBC extends java.applet.Applet {
-
- // DECLARE CONTROLS
- private boolean first = false, good_connection = false;
- private Color current_color = Color.white;
- private String dbaw_driver_name = "symantec.itools.db.jdbc.Driver";
- private String server_url = "jdbc:dbaw://localhost:8889/Watcom/SQL Anywhere 5.0 Sample/SQL Anywhere";
-
- private String db_name = "employee";
- private String first_SQL = "emp_lname";
- private String last_SQL = "emp_fname";
- private String dept_SQL = "dept_id";
- private String phone_SQL = "phone";
- private String site_SQL = "city";
- private String zip_SQL = "zip_code";
- private int max_rs_width = 15;
- private int max_wait_connect = 60;
- private Font f = new Font("default", Font.PLAIN, 12);
- private Font g = new Font("Courier", Font.PLAIN, 13);
-
- // UI (User Interface) and Thread Objects
- Button searchbutton, clearbutton;
- Checkbox quick_search;
- Choice deptchoice, sitechoice, zipchoice;
- Image go_image, stop_image;
- Label firstlabel, lastlabel, deptlabel, sitelabel;
- Label ziplabel, statuslabel, messagelabel, widthlabel, waitconnlabel;
- TextArea output;
- TextField firstfield, lastfield, passfield, waitfield, widthfield, waitconnfield;
- Thread kicker = null;
-
- // JDBC Objects
- Connection server;
- Driver dbaw_driver;
- DriverManager dmanager;
- Properties props = new Properties();
- ResultSet rs;
- Statement stmt;
-
- public void init() {
- // Set the font for everything but output
- this.setFont(f);
-
- // Initialize Background and Text Color
- this.setBackground(Color.white);
- this.setForeground(Color.red);
-
- super.init();
-
- // Initialize Buttons
- searchbutton = new Button("Search for Extension");
- clearbutton = new Button("Clear Form");
-
- // Initialize CheckBox
- quick_search = new Checkbox("Name Search");
- quick_search.setState(true);
-
- // Initialize Choices
- deptchoice = new Choice();
- deptchoice.addItem("All");
- deptchoice.select(0);
- deptchoice.disable();
-
- sitechoice = new Choice();
- sitechoice.addItem("All");
- sitechoice.select(0);
- sitechoice.disable();
-
- zipchoice = new Choice();
- zipchoice.addItem("All");
- zipchoice.select(0);
- zipchoice.disable();
-
- // Initialize Images
- go_image = Toolkit.getDefaultToolkit().getImage("go_image.gif");
- stop_image = Toolkit.getDefaultToolkit().getImage("stop_image.gif");
-
- // Initialize Labels
- firstlabel = new Label("First Name:");
- lastlabel = new Label("Last Name:");
- deptlabel = new Label("Department:");
- sitelabel = new Label("City:");
- ziplabel = new Label("Zip Code:");
- statuslabel = new Label("Status:");
- messagelabel = new Label("Feedback:");
- widthlabel = new Label("Text Width:");
- waitconnlabel = new Label("Wait to Connect (in sec):");
-
- // Initialize Properties
- props.put("user","dba");
- props.put("password","sql");
-
- // Initialize TextArea
- output = new TextArea();
- output.setEditable(false);
- output.setFont(g);
-
- // Initialize TextFields
- widthfield = new TextField();
- widthfield.setBackground(current_color);
- widthfield.setText(String.valueOf(max_rs_width));
-
- waitconnfield = new TextField();
- waitconnfield.setBackground(current_color);
- waitconnfield.setText(String.valueOf(max_wait_connect));
-
- firstfield = new TextField();
- firstfield.setBackground(current_color);
-
- lastfield = new TextField();
- lastfield.setBackground(current_color);
-
- passfield = new TextField();
- passfield.setBackground(current_color);
- passfield.disable();
-
- waitfield = new TextField();
- waitfield.setBackground(current_color);
- waitfield.disable();
- waitfield.setText("Ready.");
-
- //{{INIT_CONTROLS
- setLayout(null);
-
- // Add Checkbox
- add(quick_search);
- quick_search.reshape(insets().left+85, insets().top+5, 100, 20);
-
- // Add Width
- add(widthlabel);
- widthlabel.reshape(insets().left+195, insets().top+7, 75, 20);
- add(widthfield);
- widthfield.reshape(insets().left+270, insets().top+7, 40, 20);
-
- // Add WaitConn
- add(waitconnlabel);
- waitconnlabel.reshape(insets().left+312, insets().top+7, 147, 20);
- add(waitconnfield);
- waitconnfield.reshape(insets().left+460, insets().top+7, 40, 20);
-
- // Add First Name
- add(firstlabel);
- firstlabel.reshape(insets().left+2, insets().top+40, 80, 20);
- add(firstfield);
- firstfield.reshape(insets().left+85, insets().top+40, 160, 20);
- firstfield.requestFocus();
-
- // Add Last Name
- add(lastlabel);
- lastlabel.reshape(insets().left+250, insets().top+40, 80, 20);
- add(lastfield);
- lastfield.reshape(insets().left+335, insets().top+40, 165, 20);
-
- // Add Department
- add(deptlabel);
- deptlabel.reshape(insets().left+2, insets().top+65, 80, 20);
- add(deptchoice);
- deptchoice.reshape(insets().left+85, insets().top+65, 160, 20);
-
- // Add Site
- add(sitelabel);
- sitelabel.reshape(insets().left+250, insets().top+65, 80, 20);
- add(sitechoice);
- sitechoice.reshape(insets().left+335, insets().top+65, 165, 20);
-
- // Add zip
- add(ziplabel);
- ziplabel.reshape(insets().left+2, insets().top+90, 80, 20);
- add(zipchoice);
- zipchoice.reshape(insets().left+85, insets().top+90, 160, 20);
-
- // Add Search for Extension Button
- add(searchbutton);
- searchbutton.reshape(insets().left+260, insets().top+90, 140, 20);
-
- // Add Clear Button
- add(clearbutton);
- clearbutton.reshape(insets().left+405, insets().top+90, 95, 20);
-
- // Add Waitfield
- add(statuslabel);
- statuslabel.reshape(insets().left+2, insets().top+115, 80, 20);
- add(waitfield);
- waitfield.reshape(insets().left+85, insets().top+115, 415, 20);
-
- // Add Passfield
- add(messagelabel);
- messagelabel.reshape(insets().left+2, insets().top+140, 80, 20);
- add(passfield);
- passfield.reshape(insets().left+85, insets().top+140, 415, 20);
-
- // Add Output
- add(output);
- output.reshape(insets().left+15, insets().top+165, 485, 335);
-
- // Show opening messages
- opening();
-
- // Display stop light
- stoplight();
- repaint();
-
- show();
- //}}
- }
-
- // add_choices()
- final void add_choices() {
- searchbutton.disable();
- stoplight(); // show red light
-
- // Create statement for choices
- try {
- // create a statement to be executed from the connection
- stmt = server.createStatement();
- }
- catch (SQLException stmt_error) {
- passfield.setText("Could not create statement for department!");
- sleep();
- }
-
- // Add department choice
- try {
- waitfield.setText("Please wait while PhoneBase is loading...");
- passfield.setText("Opening database '" + db_name + "'");
-
- // Get result set
- rs = stmt.executeQuery("SELECT DISTINCT " + dept_SQL + " FROM "
- + db_name + " ORDER BY " + dept_SQL);
-
- passfield.setText("Adding department choices...");
-
- // Grab the first record. If it's null, get the next record
- // (Since they were sorted, all the null are on the top)
- // then add it to the choice box
- rs.next();
- while (rs.getString(1) == null) rs.next();
- deptchoice.addItem(rs.getString(1));
-
- // Continue to grab records and add them to the choice
- while (rs.next()) { deptchoice.addItem(rs.getString(1)); }
- deptchoice.enable();
- }
- catch (SQLException next_error) {
- passfield.setText("Problem with department choice!");
- sleep();
- }
-
- // Add site choice
- try {
- // Get result set
- rs = stmt.executeQuery("SELECT DISTINCT " + site_SQL + " FROM "
- + db_name + " ORDER BY " + site_SQL
- );
-
- passfield.setText("Adding site choices...");
-
- // Grab the first record. If it's null, get the next record
- // (Since they were sorted, all the null are on the top)
- // then add it to the choice box
- rs.next();
- while (rs.getString(1) == null) rs.next();
- sitechoice.addItem(rs.getString(1));
-
- // Continue to grab records and add them to the choice
- while (rs.next()) { sitechoice.addItem(rs.getString(1)); }
- sitechoice.enable();
- }
- catch (SQLException next_error) {
- passfield.setText("Problem with site choice!");
- sleep();
- }
-
- // Add zip choice
- try {
- // Get result set
- rs = stmt.executeQuery("SELECT DISTINCT " + zip_SQL + " FROM "
- + db_name + " ORDER BY " + zip_SQL);
-
- passfield.setText("Adding zip choice...");
-
- // Grab the first record. If it's null, get the next record
- // (Since they were sorted, all the null are on the top)
- // then add it to the choice box
- rs.next();
- while (rs.getString(1) == null) rs.next();
- zipchoice.addItem(rs.getString(1));
-
- // Continue to grab records and add them to the choice
- while (rs.next()) { zipchoice.addItem(rs.getString(1)); }
- zipchoice.enable();
- }
- catch (SQLException next_error) {
- passfield.setText("Problem with zip choice!");
- sleep();
- }
-
- // Close the statement
- try { rs.close(); stmt.close(); }
- catch (SQLException close_error) {
- passfield.setText("Could not close statement/resultset for zip");
- sleep();
- }
-
- // Reset the UI
- waitfield.setText("Ready.");
- passfield.setText("");
- searchbutton.enable();
- repaint(); // show green light
- }
-
- // close_server()
- final void close_server() {
- passfield.setText("Closing server " + server_url + "...");
- try {
- server.close();
- passfield.setText("Connection closed safely!");
-
- good_connection = false;
- }
- catch (Exception close_error) {
- passfield.setText("Could not close " + server_url);
- sleep();
- }
- }
-
- // connect_server()
- final void connect_server() {
- waitfield.setText("Busy.");
- passfield.setText("Opening server " + server_url + "...");
-
- // set the timeout so that server closes if fail to connect
- dmanager.setLoginTimeout(Integer.valueOf(waitconnfield.getText()).intValue());
-
- try {
- searchbutton.disable();
- widthfield.disable();
- waitconnfield.disable();
-
- // create a specific driver for use with JDBC
- dbaw_driver = (Driver)Class.forName(dbaw_driver_name).newInstance();
- // connect with the driver
- server = dbaw_driver.connect(server_url, props);
- passfield.setText("Opening server " + server_url + "... Connection successful!");
- good_connection = true;
- }
- catch (InstantiationException e) { passfield.setText(e.getMessage()); }
- catch (ClassNotFoundException e) { passfield.setText(e.getMessage()); }
- catch (IllegalAccessException e) { passfield.setText(e.getMessage()); }
- catch (SQLException open_error) {
- passfield.setText("Could not open server " + server_url);
- waitfield.setText("Ready.");
- searchbutton.enable();
- widthfield.enable();
- waitconnfield.enable();
- }
- }
-
- // db_search()
- final void db_search() {
- int counter = 0;
- String temp_string1, temp_string2;
- String padding = new String(" ");
-
- // Show Status
- waitfield.setText("Please wait while PhoneBase is loading...");
-
- // Print the results
- String searchfirst = new String(firstfield.getText());
- String searchlast = new String(lastfield.getText());
-
- try {
- passfield.setText("Opening database '" + db_name + "'");
- // create a statement to be executed from the connection
- stmt = server.createStatement();
-
- try {
- // fetch the new value of Text Width
- max_rs_width = Integer.valueOf(widthfield.getText()).intValue();
- // get results
- rs = stmt.executeQuery(generate_SQL());
-
- output.setText(" PhoneBase Search Result\n");
- output.appendText("========================================================\n");
-
- if (rs.next()) { // grab first record
- // add padding for textwidth
- temp_string1 = new String(rs.getString(1).concat(padding));
- temp_string1 = temp_string1.substring(0, max_rs_width);
- temp_string2 = new String(rs.getString(2).concat(padding));
- temp_string2 = temp_string2.substring(0, max_rs_width);
-
- output.appendText("Located:\n");
- output.appendText(" " + temp_string2 + ", " + temp_string1
- + " at extension " + rs.getString(3).substring(6) + "\n");
- counter++;
-
- // continue to grab records
- while(rs.next()) {
- // add padding for textwidth
- temp_string1 = rs.getString(1).concat(padding);
- temp_string1 = temp_string1.substring(0, max_rs_width);
- temp_string2 = rs.getString(2).concat(padding);
- temp_string2 = temp_string2.substring(0, max_rs_width);
-
- output.appendText(" " + temp_string2 + ", " + temp_string1
- + " at extension " + rs.getString(3).substring(6) + "\n");
- counter++;
- }
- output.appendText("\n" + counter + " hit(s) found.\n");
- }
- }
- catch (SQLException next_error) {
- passfield.setText("Problem with executing multiple statements!");
- sleep();
- }
-
- rs.close();
- stmt.close();
- }
- catch (Exception filter_error) {
- passfield.setText("Could not create/close statement!");
- sleep();
- }
-
- if (counter == 0) output.appendText("\nNo hits found.\n\n");
- output.appendText("========================================================\n");
-
- // Reset the UI
- searchbutton.enable();
- widthfield.enable();
- waitconnfield.enable();
- waitfield.setText("Ready.");
- passfield.setText("");
-
- repaint(); // show green light
- }
-
- // generate_SQL()
- final String generate_SQL() {
- // stuff needed for this method
- waitfield.setText("Search initiated...");
- String select = new String("SELECT " + first_SQL + ", " + last_SQL + ", " + phone_SQL + " ");
- String from = new String("FROM " + db_name + " ");
- boolean first_exist = false, last_exist = false, not_first_one = false;
- boolean dept_exist = false, site_exist = false, zip_exist = false;
- int wild_return = has_wildcard();
-
- // if there is something in these textfields and choices, then they exist
- if (firstfield.getText().length() != 0) first_exist = true;
- if (lastfield.getText().length() != 0) last_exist = true;
- if (deptchoice.getSelectedIndex() != 0) dept_exist = true;
- if (sitechoice.getSelectedIndex() != 0) site_exist = true;
- if (zipchoice.getSelectedIndex() != 0) zip_exist = true;
-
- // if the textfield only has '*', then they don't exist
- // it would be redundant to have firstfield to contain 'bob' and lastfield
- // to contain '*' when you could say only firstfield has 'bob'
- if (firstfield.getText().compareTo("*") == 0) first_exist = false;
- if (lastfield.getText().compareTo("*") == 0) last_exist = false;
-
- // if any of the components exist, then do a conditional search (where clause)
- if (first_exist || last_exist || dept_exist || site_exist || zip_exist) {
- from = from.concat("WHERE");
-
- // if firstfield exists and it contains no '*', do simple "where a='b'"
- // statement
- if ( (first_exist) && ((wild_return == 0) || (wild_return == 2)) ){
- not_first_one = true;
- from = from.concat(" " + first_SQL + " = '" + firstfield.getText() + "'");
- }
- // else use "where a LIKE 'b%'" statement
- else if ( (first_exist) && ((wild_return == 1) || (wild_return == 3)) ) {
- not_first_one = true;
- from = from.concat(" " + first_SQL + " LIKE '"
- + (firstfield.getText()).replace('*', '%').replace('?', '_') + "'");
- }
-
- // if lastfield exists and it contains no '*', do simple "where a='b'"
- // statement
- if ( (last_exist) && ((wild_return == 0) || (wild_return == 1)) ) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + last_SQL + " = '" + lastfield.getText() + "'");
- }
- // else use "where a LIKE 'b%'" or "where a LIKE 'b_'" statement
- else if ( (last_exist) && ((wild_return == 2) || (wild_return == 3)) ) {
- if (not_first_one) { from = from.concat(" AND"); }
- not_first_one = true;
- from = from.concat(" " + last_SQL + " LIKE '"
- + (lastfield.getText()).replace('*', '%').replace('?', '_') + "'");
- }
-
- // for rest of choices, do simple "where a = 'b'" statement
- if (dept_exist) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + dept_SQL + " = '" + deptchoice.getSelectedItem() + "'");
- }
- if (site_exist) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + site_SQL + " = '" + sitechoice.getSelectedItem() + "'");
- }
- if (zip_exist) {
- if (not_first_one) { from = from.concat(" AND"); }
- from = from.concat(" " + zip_SQL + " = '" + zipchoice.getSelectedItem() + "'");
- }
- }
- // assemble and return the SQL statement
- return (select + from);
- }
-
- // has_wildcard()
- final int has_wildcard() {
- int num_of_wildcard = 0;
-
- // if the textfield has wildcard, set it equal to a number
- // 0 = none, 1 = first, 2 = last, 3 = first and last
- if ((firstfield.getText()).indexOf("*", 0) != -1) { num_of_wildcard = 1; }
- else if ((firstfield.getText()).indexOf("?", 0) != -1) { num_of_wildcard = 1; }
- if ((lastfield.getText()).indexOf("*", 0) != -1) {
- if (num_of_wildcard == 1) num_of_wildcard = 3;
- else num_of_wildcard = 2;
- }
- else if ((lastfield.getText()).indexOf("?", 0) != -1) {
- if (num_of_wildcard == 1) num_of_wildcard = 3;
- else num_of_wildcard = 2;
- }
- return num_of_wildcard;
- }
-
- // is_empty()
- final boolean is_empty() {
- // are the textfields empty?
- if (((firstfield.getText()).length() == 0) && ((lastfield.getText()).length() == 0))
- return true;
- else return false;
- }
-
- // opening()
- final void opening() {
- output.setText("Welcome to PhoneBaseJDBC-Watcom '96 Build 17\n");
- output.appendText("========================================================\n");
- output.appendText("This program contains:\n");
- output.appendText(" - Data-aware choice components.\n");
- output.appendText(" - Supports wildcard searches via '*'.\n");
- output.appendText(" - Supports single-place wildcard searches via '?'.\n");
- output.appendText(" - Text Width adjustments.\n");
- output.appendText("========================================================\n");
- }
-
- // paint()
- public void paint(Graphics g) {
- g.drawImage(go_image, 15, 5, this);
- }
-
- // sleep()
- final void sleep() {
- waitfield.setText("Exception caught! Please notify maintenance of exception below.");
- firstfield.disable();
- this.disable();
- for (int i = 0; i < 3; i++) i = 1;
- }
-
- // stoplight()
- public void stoplight() {
- Graphics gr = this.getGraphics();
- gr.clearRect(15, 5, 28, 34);
- gr.drawImage(stop_image, 15, 5, null);
- }
-
- // handleEvent()
- public boolean handleEvent(Event event) {
- Object evt = event.target;
-
- // Display Status
- this.showStatus("Symantec Corp., 1996");
-
- if (event.id == Event.KEY_PRESS) {
- // tabbing order
- if (event.key == 9) {
- if (evt == firstfield) {lastfield.requestFocus();}
- else if ((evt == lastfield) && (deptchoice.isEnabled())) {
- deptchoice.requestFocus();
- }
- else if ((evt == lastfield) && (!deptchoice.isEnabled())) {
- firstfield.requestFocus();
- }
- else if (evt == deptchoice) {sitechoice.requestFocus();}
- else if (evt == sitechoice) {zipchoice.requestFocus();}
- else if (evt == zipchoice) {searchbutton.requestFocus();}
- else if (evt == searchbutton) {clearbutton.requestFocus();}
- else if (evt == clearbutton) {firstfield.requestFocus();}
- else if (evt == output) {firstfield.requestFocus(); }
- else { InterruptedException e; return false;}
- }
- // if press enter and textfield exists, do search
- else if ((event.key == 10) && (!is_empty())) {
- stoplight();
- if (good_connection) {db_search();} // if connection exist, do search
- else { // else, start the connection
- connect_server();
- if (good_connection) {db_search();}
- }
- }
- else return super.handleEvent(event);
- return true;
- }
- // clear or reset everything
- else if ((event.id == Event.ACTION_EVENT) && (evt instanceof Button)) {
- if (evt == clearbutton) {
- firstfield.setText("");
- lastfield.setText("");
- deptchoice.select(0);
- sitechoice.select(0);
- zipchoice.select(0);
- firstfield.requestFocus();
- }
- // if click enter and textfield exists, do search
- else if ((evt == searchbutton) && (!is_empty())) {
- stoplight();
- if (good_connection) {db_search();} // if connection exist, do search
- else { // else, start the connection
- connect_server();
- if (good_connection) {db_search();}
- }
- }
- else { InterruptedException e; return false; }
- return true;
- }
- else if ((event.id == Event.ACTION_EVENT) && (evt instanceof Checkbox)) {
- // if checkbox is unchecked, add choices
- if ((evt == quick_search) && (quick_search.getState() == false)) {
- quick_search.setState(false);
- if (!good_connection) { connect_server();} // if connection exist, do search
- add_choices(); // get choice information
- firstfield.requestFocus();
- }
- // if checkbox is checked, disable choices
- if ((evt == quick_search) && (quick_search.getState() == true)) {
- quick_search.setState(true);
- searchbutton.disable();
- // reset choices to 'all'
- deptchoice.select(0);
- deptchoice.disable();
- sitechoice.select(0);
- sitechoice.disable();
- zipchoice.select(0);
- zipchoice.disable();
-
- searchbutton.enable();
- }
- else { InterruptedException e; return false; }
- return true;
- }
- else if (event.id == Event.WINDOW_DESTROY) {
- close_server();
- kicker = null;
- super.stop();
- System.exit(0);
- return true;
- }
- else return super.handleEvent(event);
- }
- }
-