home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 28.0 KB | 802 lines |
-
- import java.awt.*;
- import java.applet.*;
- import java.sql.*;
- import java.util.*;
-
- //08-02-96 12:03pm08-02-96 12:03pm
- //Greg Kim
-
- // This class was basically created to encapsulate data and stuff.
- // Methods should hopefully correspond to what a really dataBase
- // should be capable of performing (Of course just THE basic stuff
- // e.g. nextRecord() previousRecord(), search(), etc), nothing fancy here
-
- class DataBase {
- private String serverUrl;
- // check for connection.
- private boolean isConnection;
- Driver driver;
- DriverManager dmanager;
- Connection server;
- Properties props;
- ResultSet resultSet;
- // check if the resultSet is in ascending order w/respect to id
- boolean isAscending;
-
-
- DataBase(String hostname, String user, String password) {
- serverUrl = hostname;
- isConnection = false;
- props = new Properties();
- props.put("user", user);
- props.put("password", password);
- resultSet = null;
- isAscending = true;
-
- }
-
- void connectServer(String driver_name) {
- // Use the driver_name driver to connect to the data base server.
- try {
- driver =
- (Driver)Class.forName(driver_name).newInstance();
- dmanager.registerDriver(driver);
- server = dmanager.getConnection(serverUrl, props);
- }
- catch (SQLException e) {
- System.out.println("Exception from DataBase.connectServer() : " +
- e.getMessage());
- }
- catch (InstantiationException e) {
- System.out.println("Exception from DataBase.connectServer() : " +
- e.getMessage());
- }
- catch (ClassNotFoundException e) {
- System.out.println("Exception from DataBase.connectServer() : " +
- e.getMessage());
- }
- catch (IllegalAccessException e) {
- System.out.println("Exception from DataBase.connectServer() : " +
- e.getMessage());
- }
- isConnection = true;
- }
-
- void closeServer() {
- // disconnect with data base server.
- try {
- server.close();
- }
- catch (Exception e){
- System.out.println("Exception from DataBase.closeServer() : " +
- e.getMessage());
- }
- isConnection = false;
- }
-
- ResultSet search(String sqlStmt) {
- // Excecute sqlStmt which will most likely be a "Select * from product ..." of some
- // sort returning the resultSet. ResultSet will contain all records in the table.
- try{
- Statement stmt = server.createStatement();
- resultSet = stmt.executeQuery(sqlStmt);
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.search() : " +
- e.getMessage());
- }
-
- return resultSet;
-
- }
-
- void goNext(String id) {
- // Scroll to the next record w/ respect to id's in acending order.
-
- if(isAscending) {
- // if the resultSet is already in ascending order, simply do a next() on the
- // resultSet.
-
- try {
- resultSet.next();
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.goNext() : " +
- e.getMessage());
- }
- }
- else {
- //if the resultSet is in descending order, produce a new resultSet
- //in ascending order and do a orderChange().
- orderChange();
- //re-make the resultSet in ascending order
- search("SELECT * FROM product ORDER BY id ASC");
- Integer currentVal = Integer.valueOf(id);
-
- try {
- //move the cursor to the first valid record
- resultSet.next();
- while(currentVal.intValue() != resultSet.getInt("id")){
- resultSet.next();
- }
-
- resultSet.next();
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.goNext() : " +
- e.getMessage());
- }
- }
- }
-
- void goPrevious(String id) {
- // go to the previous record w/ respect to product id's in ascending order.
- if(!isAscending) {
- //if the resultSet is already in descending order, simply call next() on the
- // resultSet.
- try {
- resultSet.next();
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.goPrevious() : " +
- e.getMessage());
- }
- }
- else {
-
- orderChange();
- //re-make the resultSet in descending order.
- search("SELECT * FROM product ORDER BY id DESC");
-
- Integer currentVal = Integer.valueOf(id);
-
- try {
- //move cursor to first valid record
- resultSet.next();
-
- while(currentVal.intValue() != resultSet.getInt("id")){
- resultSet.next();
- }
-
- resultSet.next();
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.goPrevious() : " +
- e.getMessage());
- }
- }
- }
-
-
- void showTable(TextField[] textFieldList, int textFieldColList[],
- Choice[] choiceList, int choiceColList[])
- // traverve the list of textFields and choices and show'em on our applet.
- // note: if we were to have check boxes on this applet we would have to
- // include that in our parameter list as well.
- {
- // make prodIDTextField uneditable before showing.
- textFieldList[0].setEditable(false);
- showTextFields(textFieldList, textFieldColList);
- showChoice(choiceList, choiceColList);
- }
-
-
- void showTextFields(TextField[] textFieldList, int columnList[]) {
- // Show all records tied to textFields on our applet.
-
- int n = textFieldList.length;
- for(int i = 0; i < n; i++) {
- try{
- textFieldList[i].setText(resultSet.getString(columnList[i]));
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.showTextFields() : " +
- e.getMessage());
- }
- }
- }
-
-
- void showChoice(Choice[] choiceList, int colList[]) {
- // Show all records tied to choiceBoxes on our applet.
- int n = choiceList.length;
- for(int i = 0; i < n; i++) {
- try {
- choiceList[i].select(resultSet.getString(colList[i]));
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.showChoice() : " +
- e.getMessage());
- }
- }
- }
-
- String currentID()
- {
- // this is just a little helper method to extract the current product id on
- // our current record.
-
- String returnStrg = null;
-
- try {
- returnStrg = new String(resultSet.getString("id"));
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.currentID() : " +
- e.getMessage());
- }
- return returnStrg;
-
- }
-
- void orderChange()
- {
- // called every time we change the order of our resultSet.
- if(isAscending)
- isAscending = false;
- else
- isAscending = true;
- }
-
- void clear(TextField[] textFieldList)
- {
- // clears all text in all the textFields.
- int n = textFieldList.length;
- String emptyString = new String();
-
- for(int i = 0; i < n; i++) {
- textFieldList[i].setText(emptyString);
- }
- }
-
- void goTo(String id)
- {
- //Goto the record indexed by id.
- Integer currentVal = Integer.valueOf(id);
-
- try {
- //move cursor to first valid record
- resultSet.next();
-
- while(currentVal.intValue() != resultSet.getInt("id")){
- resultSet.next();
- }
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.goTo() : " +
- e.getMessage());
- }
- }
-
- void commit()
- {
- // we call this every time we want to modify the data base.
- try{
- Statement stmt = server.createStatement();
- stmt.executeQuery("COMMIT WORK");
- }
- catch(SQLException e) {
- System.out.println("Exception from DataBase.commit() : " +
- e.getMessage());
- }
-
- }
-
- }
-
-
-
- public class inventory extends Applet {
-
- public void init() {
-
- super.init();
-
-
- // Connection Initializtion.
- dataBase = new
- DataBase("jdbc:scale://localhost:8889/Watcom/Sample/Sample", "dba", "sql");
- dataBase.connectServer("symantec.itools.db.jdbc.Driver");
- // start off by generating all records in the product table
- dataBase.search("SELECT * FROM product ORDER BY id ASC");
-
- setBackground(Color.lightGray);
-
- //{{INIT_CONTROLS
- setLayout(null);
- resize(546,395);
- titleLabel=new Label("Inventory Applet");
- titleLabel.setFont(new Font("TimesRoman",Font.BOLD|Font.ITALIC,22));
- add(titleLabel);
- titleLabel.reshape(191,23,168,30);
- prodIDTextField=new TextField(11);
- add(prodIDTextField);
- prodIDTextField.reshape(91,75,91,23);
- prodIDLabel=new Label("Prod. ID:");
- prodIDLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(prodIDLabel);
- prodIDLabel.reshape(0,75,77,23);
- nameLabel=new Label("Name:");
- nameLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(nameLabel);
- nameLabel.reshape(0,105,63,23);
- nameTextField=new TextField(12);
- add(nameTextField);
- nameTextField.reshape(91,105,105,23);
- descpLabel=new Label("Description:");
- descpLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(descpLabel);
- descpLabel.reshape(0,135,91,15);
- descpTextField=new TextField(44);
- add(descpTextField);
- descpTextField.reshape(91,135,371,23);
- sizeLabel=new Label("Size:");
- sizeLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(sizeLabel);
- sizeLabel.reshape(0,165,70,15);
- sizeTextField=new TextField(15);
- add(sizeTextField);
- sizeTextField.reshape(91,165,126,23);
- colorLabel=new Label("Color:");
- colorLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(colorLabel);
- colorLabel.reshape(0,195,70,15);
- colorChoiceBox= new Choice();
- add(colorChoiceBox);
- colorChoiceBox.reshape(91,195,112,98);
- colorChoiceBox.addItem("Black");
- colorChoiceBox.addItem("White");
- colorChoiceBox.addItem("Blue");
- colorChoiceBox.addItem("Red");
- colorChoiceBox.addItem("Yellow");
- colorChoiceBox.addItem("Violet");
- quanLabel=new Label("Quantity:");
- quanLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(quanLabel);
- quanLabel.reshape(0,225,70,23);
- quanTextField=new TextField(9);
- add(quanTextField);
- quanTextField.reshape(91,225,77,23);
- priceLabel=new Label("Unit Price:");
- priceLabel.setFont(new Font("TimesRoman",Font.BOLD,14));
- add(priceLabel);
- priceLabel.reshape(0,255,84,15);
- priceTextField=new TextField(11);
- add(priceTextField);
- priceTextField.reshape(91,255,98,23);
- searchLabel=new Label("Search By:");
- searchLabel.setFont(new Font("TimesRoman",Font.BOLD|Font.ITALIC,14));
- add(searchLabel);
- searchLabel.reshape(203,255,77,23);
- searchChoiceBox= new Choice();
- add(searchChoiceBox);
- searchChoiceBox.reshape(280,255,91,75);
- searchChoiceBox.addItem("Prod. ID#");
- searchChoiceBox.addItem("Name");
- searchTextField=new TextField(11);
- add(searchTextField);
- searchTextField.reshape(371,255,98,23);
-
- search=new Button("Search");
- add(search);
- search.reshape(476,255,56,23);
-
- next=new Button(">");
- next.setFont(new Font("Dialog",Font.BOLD,18));
- add(next);
- next.reshape(42,285,91,30);
-
- first=new Button("<<");
- first.setFont(new Font("Dialog",Font.BOLD,18));
- add(first);
- first.reshape(154,285,91,30);
-
- New=new Button("New");
- New.setFont(new Font("TimesRoman",Font.BOLD|Font.ITALIC,18));
- add(New);
- New.reshape(294,285,91,30);
-
- Update=new Button("Update");
- Update.setFont(new Font("TimesRoman",Font.BOLD|Font.ITALIC,18));
- add(Update);
- Update.reshape(406,285,91,30);
-
- previous=new Button("<");
- previous.setFont(new Font("Dialog",Font.BOLD,18));
- add(previous);
- previous.reshape(42,322,91,30);
-
- last=new Button(">>");
- last.setFont(new Font("Dialog",Font.BOLD,18));
- add(last);
- last.reshape(154,322,91,30);
-
- add=new Button("Add");
- add.setFont(new Font("TimesRoman",Font.BOLD|Font.ITALIC,18));
- add(add);
- add.reshape(294,322,91,30);
-
- close=new Button("Close");
- close.setFont(new Font("TimesRoman",Font.BOLD|Font.ITALIC,18));
- add(close);
- close.reshape(406,322,91,30);
-
-
-
- //}}
-
- //move cursor to the first valid record.
- dataBase.goNext(prodIDTextField.getText());
-
-
- // one cheesy way to have global (w/ respect to only this class) variables
- // with initializers.
- TextField textFieldListLocal[] = {prodIDTextField, nameTextField, descpTextField,
- sizeTextField, quanTextField, priceTextField};
- textFieldList = textFieldListLocal;
-
- int columnListLocal[] = {1, 2, 3, 4, 6, 7};
- columnList = columnListLocal;
-
- Choice choiceListLocal[] = {colorChoiceBox};
- choiceList = choiceListLocal;
-
- int choiceColListLocal[] = {5};
- choiceColList = choiceColListLocal;
-
- // we want the id textField to be uneditable.
- prodIDTextField.setEditable(false);
-
- //Show the first record in our table.
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
-
-
- }
-
-
-
- public boolean handleEvent(Event event) {
- if((event.target == next) && (event.id == Event.ACTION_EVENT)) {
-
- dataBase.goNext(dataBase.currentID());
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
- }
-
- else if((event.target == previous) && (event.id == Event.ACTION_EVENT)) {
-
- dataBase.goPrevious(dataBase.currentID());
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
- }
-
- else if((event.target == first) && (event.id == Event.ACTION_EVENT)) {
- if(dataBase.isAscending)
- //do nothing.
- ;
- else
- dataBase.orderChange();
- // generate a new resultSet in ascending order and show the first record
- // of this resultSet
- dataBase.search("SELECT * FROM product ORDER BY id ASC");
- try { dataBase.resultSet.next(); }
- catch(SQLException e) { System.out.println("Exception caught when " +"<< " +
- "button was struck : " + e.getMessage()); }
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
- }
-
- else if((event.target == last) && (event.id == Event.ACTION_EVENT)) {
- if(dataBase.isAscending)
- dataBase.orderChange();
- else
- //do nothing.
- ;
-
- // generate a new resultSet in descending order and show the first record
- // of this resultSet
- dataBase.search("SELECT * FROM product ORDER BY id DESC");
- try { dataBase.resultSet.next(); }
- catch (SQLException e) {System.out.println("Exception caught when " +">> " +
- "button was struck : " +e.getMessage()); }
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
- }
-
- else if((event.target == New) && (event.id == Event.ACTION_EVENT)) {
-
- prodIDTextField.setEditable(true);
- add.enable();
- dataBase.clear(textFieldList);
- }
-
- else if((event.target == close) && (event.id == Event.ACTION_EVENT)) {
- dataBase.closeServer();
- }
-
- else if((event.target == add) && (event.id == Event.ACTION_EVENT)) {
- String comma = new String(",");
- String quote = new String("'");
-
- // Some error checking: Illegal user input.
- if (!isNumeric(prodIDTextField.getText())) {
- getAppletContext().showStatus("product ID must be a number!!");
- prodIDTextField.setText("");
- return false;
- }
- else if(!isNumeric(quanTextField.getText())) {
- getAppletContext().showStatus("quantity must be a number!");
- quanTextField.setText("");
- return false;
- }
- else if (!isNumeric(priceTextField.getText())) {
- getAppletContext().showStatus("Price must be a number !!");
- priceTextField.setText("");
- return false;
- }
-
- String sqlStmt = new String ("INSERT INTO product" + " (" + "id" + comma +
- "name " + comma + " description " + comma +
- " size " + comma + " color " + comma +
- " quantity " + comma + " unit_price " + ") " +
- " VALUES " + "( " + prodIDTextField.getText() + comma
- + quote + nameTextField.getText() + quote +
- comma + quote + descpTextField.getText() + quote +
- comma + quote + sizeTextField.getText() + quote +
- comma + quote +
- colorChoiceBox.getSelectedItem() + quote + comma +
- quanTextField.getText() + comma +
- priceTextField.getText() + ")" );
-
- try{
- Statement stmt = dataBase.server.createStatement();
- stmt.executeQuery(sqlStmt);
- }
- catch(SQLException e) {
- System.out.println("Exception caught when add button was stuck : " +
- e.getMessage());
- }
- // commit to our changes.
- dataBase.commit();
-
- dataBase.isAscending = true;
- dataBase.search("SELECT * from product ORDER BY id ASC");
- dataBase.goTo(prodIDTextField.getText());
- dataBase.add.disable();
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
- }
-
- else if((event.target == Update) && (event.id == Event.ACTION_EVENT)) {
- String quote = new String("'");
-
- // Some error checking: Illegal User input.
- if(!isNumeric(quanTextField.getText())) {
- getAppletContext().showStatus("quantity must be a number!");
- quanTextField.setText("");
- return false;
- }
- else if (!isNumeric(priceTextField.getText())) {
- getAppletContext().showStatus("Price must be a number !!");
- priceTextField.setText("");
- return false;
- }
- String sqlStmt = new String("UPDATE product SET name = " +
- quote + nameTextField.getText() +
- quote + " , " + "description = " + quote +
- descpTextField.getText() + quote + " , " +
- "size = " + quote +
- sizeTextField.getText() + quote +
- " , " + "quantity = " +
- quanTextField.getText() + " , " + "color = " + quote +
- colorChoiceBox.getSelectedItem() + quote + " , " +
- "unit_price = " + priceTextField.getText() +
- " WHERE id = " + dataBase.currentID());
-
- try{
- Statement stmt = dataBase.server.createStatement();
- stmt.executeQuery(sqlStmt);
- }
- catch(SQLException e) {
- System.out.println("Exception caught when update button was struck : " +
- e.getMessage());
- }
- dataBase.commit();
-
- dataBase.isAscending = true;
- dataBase.search("SELECT * from product ORDER BY id ASC");
- dataBase.goTo(prodIDTextField.getText());
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
-
-
- }
- else if((event.target == search) && (event.id == Event.ACTION_EVENT)) {
- String key = searchTextField.getText();
- String emptyString = new String();
- String sqlStmt = null;
-
-
- if(key.equals(emptyString)) {
- //if the textfield contains nothing, do nothing.
- return false;
- }
- else if((searchChoiceBox.getSelectedItem()).equals("Name")) {
- // if search by "Name"
- String subSqlStmt;
- String prodID;
- String quote = new String("'");
-
- subSqlStmt = new String("SELECT * FROM product WHERE name = " +
- quote + key + quote + " ORDER BY id ASC");
- //System.out.println("subSqlStmt is: " + subSqlStmt);
-
- ResultSet resTmp =
- dataBase.search(subSqlStmt);
- try {
- resTmp.next();
- prodID = new String(resTmp.getString(1));
- //System.out.println(prodID);
- sqlStmt = new String("SELECT * FROM product WHERE id >=" +
- prodID + " ORDER BY id ASC");
-
- }
- catch(SQLException e) {
- System.out.println("Exception caught when search button was struck : "
- + e.getMessage());
- }
- }
- else { // must be performing a search with respect to prod ID.
-
- // Some error checking: Illegal user input.
- if (!isNumeric(searchTextField.getText())) {
- getAppletContext().showStatus("product ID must be a number!!");
- searchTextField.setText("");
- return false;
- }
-
- sqlStmt = new String("SELECT * FROM product WHERE id >= "
- + key + " ORDER BY id ASC");
- }
- //System.out.println(sqlStmt);
- dataBase.search(sqlStmt);
-
- try {dataBase.resultSet.next(); }
- catch(SQLException e) {
- System.out.println("Exception caught when search button was struck : " +
- e.getMessage());
- }
-
- dataBase.showTable(textFieldList, columnList, choiceList, choiceColList);
-
- }
- else if (event.id == Event.WINDOW_DESTROY) {
- dataBase.closeServer();
- System.exit(0);
- }
- showDefaultStatus();
- return super.handleEvent(event);
-
- }
-
- //Tabbing stuff...
-
- public void start() {
- prodIDTextField.requestFocus();
- }
-
- public boolean keyDown(Event evt, int key) {
- if ((key==9) || (key==10)) {
- tab(evt);
- return true;
- }
- else return super.keyDown(evt, key);
- }
-
- void tab(Event evt) {
- Component temp = (Component)evt.target;
- boolean shiftpressed=evt.shiftDown();
-
- int index = tabindex(temp, shiftpressed);
- Component newtab = temp.getParent().getComponent(index);
- newtab.requestFocus();
- if (newtab instanceof TextField)
- ((TextField)newtab).selectAll();
- }
-
- int tabindex(Component current, boolean shiftpressed) { //Returns the tab index of the next component
- Component[] temp;
- temp = current.getParent().getComponents();
- int increment;
- boolean foundit=false;
-
- if (!(shiftpressed))
- {
- increment=1;
- for (int i=0;i<(temp.length)-1;i++)
- {
- if (current == temp[i]) foundit=true;
- if (foundit)
- {
- Component next=temp[i+increment];
- if ((next instanceof TextComponent) || (next instanceof Choice) || (next instanceof Button) || (next instanceof List))
- return (i+increment);
- else if (next instanceof Checkbox)
- {
- Checkbox tempcheckbox=(Checkbox)next;
- if (tempcheckbox.getCheckboxGroup()==null) return (i+increment);
- }
- }
- }
- }
- else
- {
- increment=-1;
- for (int i=(temp.length)-1;i>=1;i--) {
- if (current == temp[i]) foundit=true;
- if (foundit)
- {
- Component next=temp[i+increment];
- if ((next instanceof TextComponent) || (next instanceof Choice) || (next instanceof Button) || (next instanceof List))
- return (i+increment);
- else if (next instanceof Checkbox)
- {
- Checkbox tempcheckbox=(Checkbox)next;
- if (tempcheckbox.getCheckboxGroup()==null) return (i+increment);
- }
- }
- }
- }
- if (shiftpressed) {return (temp.length)-1;} else return 0; //Base case or if we've hit the final component
- }
- // end of tabbing stuff.
-
- // method to check if a string is numeric.
- boolean isNumeric(String number) {
-
- try {
- Integer val = new Integer(number);
- return true;
- }
- catch(NumberFormatException e) {
- return false;
- }
- }
-
- void showDefaultStatus() {
- getAppletContext().showStatus("Symantec Corp., 1996");
- }
-
- //{{DECLARE_CONTROLS
- Label titleLabel;
- TextField prodIDTextField;
- Label prodIDLabel;
- Label nameLabel;
- TextField nameTextField;
- Label descpLabel;
- TextField descpTextField;
- Label sizeLabel;
- TextField sizeTextField;
- Label colorLabel;
- Choice colorChoiceBox;
- Label quanLabel;
- TextField quanTextField;
- Label priceLabel;
- TextField priceTextField;
- Label searchLabel;
- Choice searchChoiceBox;
- TextField searchTextField;
- Button next;
- Button previous;
- Button first;
- Button last;
- Button New;
- Button close;
- Button add;
- Button search;
- Button Update;
- //}}
-
-
- DataBase dataBase;
- TextField textFieldList[];
- int columnList[];
- Choice choiceList[];
- int choiceColList[];
- }
-