Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import java.util.Date;
- import java.util.Calendar;
- import java.text.DateFormat;
- public class WrigleyInc extends Applet implements ItemListener, ActionListener
- {
- //Declare variables and assign initial value
- Calendar calCurrent = Calendar.getInstance();
- boolean blnSuccessfulOpen = false;
- float fltTotalCost = 0;
- int intDay = calCurrent.get(Calendar.DATE); //gets day of month
- int intMonth = calCurrent.get(Calendar.MONTH) + 1; //gets month
- int intYear = calCurrent.get(Calendar.YEAR); //gets year
- Font fntName = new Font("Cambria", Font.BOLD, 12); //sets font to Cambria and bold with 12font size
- Connection conCustomers;
- Statement cmdCustomers;
- ResultSet rsCustomers;
- Color bgColor;
- Label lblToday = new Label(" ");
- Label lblTime = new Label(" ");
- Label lblDifference = new Label(" ");
- Label lblDayOfWeek = new Label(" ");
- Choice chMonth1 = new Choice();
- Choice chMonth2 = new Choice();
- Choice lstNames = new Choice();
- TextField txtCustomerID = new TextField(15);
- TextField txtMonth1 = new TextField(10);
- TextField txtDay1 = new TextField(2);
- TextField txtCustomerName = new TextField(15);
- TextField txtAddress = new TextField(15);
- TextField txtCity = new TextField(15);
- TextField txtState = new TextField(2);
- TextField txtZip = new TextField(5);
- TextField txtPhone = new TextField(15);
- TextField txtProductName = new TextField(15);
- TextField txtOrderNumber = new TextField(15);
- TextField txtQuantity = new TextField(15);
- TextField txtUnitCost = new TextField(15);
- TextField txtTotalCost = new TextField(15);
- TextField txtID = new TextField(15);
- TextArea txaReport = new TextArea (" Customer Invoice ",20,30);
- Button btnAdd = new Button("Add");
- Button btnEdit = new Button("Save");
- Button btnCancel = new Button("Cancel");
- Button btnDelete = new Button("Delete");
- Button btnReport = new Button("Report");
- Button btnNext = new Button(" > ");
- Button btnLast = new Button(" >>> ");
- Button btnPrevious = new Button(" < ");
- Button btnFirst = new Button(" <<< ");
- JPanel pnlTxtFields = new JPanel() ;
- private String dbURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Customers.mdb;DriverID=22;}";
- public void init()
- {
- setSize(290,780) ;
- //Set up a Calendar for the current locale and time zone
- Calendar calToday = Calendar.getInstance();
- //Format today's date
- DateFormat defaultDate = DateFormat.getDateInstance();
- lblToday.setText(defaultDate.format(calToday.getTime()));
- //Set day of week
- String[] strDay = {"Sunday", "Monday", "Tuesday", "Wednesday",
- "Thursday", "Friday", "Saturday"};
- int intDayOfWeek = calToday.get(calToday.DAY_OF_WEEK) - 1;
- lblDayOfWeek.setText(strDay[intDayOfWeek]);
- //Format time
- DateFormat shortTime = DateFormat.getTimeInstance(DateFormat.SHORT);
- lblTime.setText(shortTime.format(calToday.getTime()));
- pnlTxtFields.setLayout(new GridLayout(12,2)) ;
- add(lblDayOfWeek);
- add(lblToday);
- add(lblTime);
- bgColor = Color.gray;
- setBackground(bgColor);
- //Set up panel and load database
- LoadDatabase();
- if (blnSuccessfulOpen)
- {
- //Sets up the userinterface
- lstNames.insert("Select an ID",0);
- add(lstNames);
- lstNames.addItemListener(this);
- add(pnlTxtFields) ;
- pnlTxtFields.add(new Label("Customer ID"));
- pnlTxtFields.add(txtCustomerID);
- txtCustomerID.setFont(fntName);
- pnlTxtFields.add(new Label("Customer Name"));
- txtCustomerName.setFont(fntName);
- pnlTxtFields.add(txtCustomerName);
- pnlTxtFields.add(new Label("Address"));
- txtAddress.setFont(fntName);
- pnlTxtFields.add(txtAddress);
- pnlTxtFields.add(new Label("City"));
- txtCity.setFont(fntName);
- pnlTxtFields.add(txtCity);
- pnlTxtFields.add(new Label("State"));
- txtState.setFont(fntName);
- pnlTxtFields.add(txtState);
- pnlTxtFields.add(new Label("Zip"));
- txtZip.setFont(fntName);
- pnlTxtFields.add(txtZip);
- pnlTxtFields.add(new Label("Phone"));
- txtPhone.setFont(fntName);
- pnlTxtFields.add(txtPhone);
- pnlTxtFields.add(new Label("Product Name"));
- txtProductName.setFont(fntName);
- pnlTxtFields.add(txtProductName);
- pnlTxtFields.add(new Label("Order Number"));
- txtOrderNumber.setFont(fntName);
- pnlTxtFields.add(txtOrderNumber);
- pnlTxtFields.add(new Label("Quanity"));
- txtQuantity.setFont(fntName);
- pnlTxtFields.add(txtQuantity);
- pnlTxtFields.add(new Label("Unit Cost"));
- txtUnitCost.setFont(fntName);
- pnlTxtFields.add(txtUnitCost);
- pnlTxtFields.add(new Label ("Total Cost"));
- txtTotalCost.setFont(fntName);
- pnlTxtFields.add(txtTotalCost);
- //Add/Edit/Delete/Cance/Report buttons
- setTextToNotEditable();
- add(btnAdd);
- btnAdd.addActionListener(this);
- add(btnEdit);
- btnEdit.addActionListener(this);
- add(btnDelete);
- btnDelete.addActionListener(this);
- add(btnCancel);
- btnCancel.addActionListener(this);
- btnCancel.setEnabled(false);
- add(btnReport);
- btnReport.addActionListener(this);
- add(txaReport);
- //Navigate Buttons
- add(btnFirst);
- btnFirst.addActionListener(this);
- add(btnPrevious);
- btnPrevious.addActionListener(this);
- add(btnNext);
- btnNext.addActionListener(this);
- add(btnLast);
- btnLast.addActionListener(this);
- }
- else
- {
- System.err.println("Unable to populate fields");
- }
- }
- public void LoadDatabase()
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Sun driver
- }
- catch (ClassNotFoundException err)
- {
- System.err.println("Driver didn't load");
- }
- try
- {
- //Connect to the database
- conCustomers = DriverManager.getConnection(dbURL);
- }
- catch(SQLException error)
- {
- System.err.println("Unable to Connect to Database");
- }
- try
- {
- Statement cmdCustomers = conCustomers.createStatement();
- //Create the ResultSet
- ResultSet rsCustomers = cmdCustomers.executeQuery("Select * from Customers");
- loadNames(rsCustomers);
- blnSuccessfulOpen = true;
- }
- catch(SQLException error)
- {
- System.err.println("Error in recordset");
- }
- }
- public void loadNames(ResultSet rsCustomers)
- {
- //Fill CustomerID list box
- try
- {
- while(rsCustomers.next())
- lstNames.add(rsCustomers.getString("CustomerID"));
- }
- catch (SQLException error)
- {
- System.err.println("Error in Display Record");
- }
- }
- public void itemStateChanged(ItemEvent event)
- {
- //Retrieve and display the selected record
- String strCustomerID = lstNames.getSelectedItem();
- showStatus(""); //Delete instructions
- try
- {
- Statement cmdCustomers = conCustomers.createStatement();
- rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
- txtCustomerID.setText(strCustomerID);
- displayRecord(rsCustomers);
- setTextToEditable();
- }
- catch(SQLException error)
- {
- showStatus("Error in recordset");
- }
- }
- public void displayRecord(ResultSet rsCustomers)
- {
- //Display the current record
- try
- {
- if(rsCustomers.next())
- {
- txtCustomerName.setText(rsCustomers.getString("CustomerName"));
- txtAddress.setText(rsCustomers.getString("Address"));
- txtCity.setText(rsCustomers.getString("City"));
- txtState.setText(rsCustomers.getString("State"));
- txtZip.setText(rsCustomers.getString("Zip"));
- txtPhone.setText(rsCustomers.getString("PhoneNumber"));
- txtProductName.setText(rsCustomers.getString("ProductName"));
- txtOrderNumber.setText(rsCustomers.getString("OrderNumber"));
- txtQuantity.setText(rsCustomers.getString("Quantity"));
- txtUnitCost.setText(rsCustomers.getString("UnitCost"));
- float fltQuantity = Float.valueOf(txtQuantity.getText()).floatValue();
- float fltUnitCost = Float.valueOf(txtUnitCost.getText()).floatValue();
- fltTotalCost = fltQuantity*fltUnitCost;
- txtTotalCost.setText(" " + fltTotalCost);
- showStatus("");
- }
- else
- {
- showStatus("Record not found");
- ClearTextFields();
- }
- }
- catch (SQLException error)
- {
- showStatus("Error in display record");
- }
- }
- public void actionPerformed(ActionEvent event)
- {
- //Test the command buttons
- Object objSource = event.getSource();
- if(objSource == btnAdd && event.getActionCommand() == "Add")
- Add();//setup interface before we add...
- else if (objSource == btnAdd && event.getActionCommand() == "OK")//when we click the "OK" button
- Save();
- else if(objSource == btnEdit)
- Edit();
- else if(objSource == btnDelete)
- Delete();
- else if(objSource == btnCancel)
- Cancel();
- else if(objSource == btnFirst)
- firstRecord();
- else if(objSource == btnNext)
- nextRecord();
- else if(objSource == btnPrevious)
- previousRecord();
- else if(objSource == btnLast)
- lastRecord();
- Object buttonSelected = event.getSource();
- if (buttonSelected == btnReport)
- {
- txaReport.append(
- "\n" + "\n Personal Information" + "\n" +txtCustomerName.getText() +
- "\n" +txtCustomerID.getText() + "\n" +txtAddress.getText() +
- "\n" +txtCity.getText()+ ", " + txtState.getText()+ " " +
- txtZip.getText()+ "\n Order History" + "\n" +txtProductName.getText() + "\n" +
- txtOrderNumber.getText() + "\n" +txtQuantity.getText() + "\n$" +
- txtTotalCost.getText() );
- }
- }
- public void setTextToNotEditable()
- {
- //Lock the text fields
- txtCustomerID.setEditable(false);
- txtCustomerName.setEditable(false);
- txtAddress.setEditable(false);
- txtCity.setEditable(false);
- txtState.setEditable(false);
- txtZip.setEditable(false);
- txtPhone.setEditable(false);
- txtProductName.setEditable(false);
- txtQuantity.setEditable(false);
- txtOrderNumber.setEditable(false);
- txtUnitCost.setEditable(false);
- txtTotalCost.setEditable(false);
- }
- public void setTextToEditable()
- {
- //Unlock the text fields
- txtCustomerID.setEditable(true);
- txtCustomerName.setEditable(true);
- txtAddress.setEditable(true);
- txtCity.setEditable(true);
- txtState.setEditable(true);
- txtZip.setEditable(true);
- txtPhone.setEditable(true);
- txtProductName.setEditable(true);
- txtQuantity.setEditable(true);
- txtOrderNumber.setEditable(true);
- txtUnitCost.setEditable(true);
- txtTotalCost.setEditable(true);
- }
- public void ClearTextFields()
- {
- //Clear the Text Fields
- txtCustomerID.setText("");
- txtCustomerName.setText("");
- txtAddress.setText("");
- txtCity.setText("");
- txtState.setText("");
- txtZip.setText("");
- txtPhone.setText("");
- txtProductName.setText("");
- txtOrderNumber.setText("");
- txtID.setText("");
- txtQuantity.setText("");
- txtUnitCost.setText("");
- txtTotalCost.setText("");
- }
- public void Add()
- {
- //Add a new record
- showStatus("");
- //Empty the text fields
- setTextToEditable();
- ClearTextFields();
- txtCustomerID.requestFocus ();
- //Change the button labels
- btnAdd.setLabel("OK");
- btnCancel.setEnabled(true);
- //Disable the Delete and Edit buttons
- btnDelete.setEnabled(false);
- btnEdit.setEnabled(false);
- }
- public void Save()
- {
- String strCustomerID = lstNames.getSelectedItem();
- //Save the new record
- // Activated when Add button has an "OK" label
- if (txtCustomerID.getText().length() == 0 || txtCustomerName.getText().length() == 0)
- showStatus("The Customer Name or ID is blank");
- else
- {
- try
- {
- Statement cmdCustomers = conCustomers.createStatement();
- cmdCustomers.executeUpdate( "Insert Into Customers "
- + "( CustomerName, CustomerID, PhoneNumber, Address, City, State, Zip) "
- + "Values('"
- + txtCustomerName.getText() + "', '"
- + txtCustomerID.getText() + "', '"
- + txtPhone.getText() + "', '"
- + txtAddress.getText() + "', '"
- + txtCity.getText() + "', '"
- + txtState.getText() + "', '"
- + txtZip.getText() + "')");
- Statement cmdCustomers2 = conCustomers.createStatement();
- cmdCustomers2.executeUpdate( "Insert Into Orders "
- + "( ProductName, ID, OrderNumber, Quantity, UnitCost) "
- + "Values('"
- + txtProductName.getText() + "', '"
- + txtID.getText() + "', '"
- + txtOrderNumber.getText() + "', '"
- + txtQuantity.getText() + "', '"
- + txtUnitCost.getText() + "')");
- //Add to ID list
- lstNames.add(txtCustomerID.getText());
- //Reset buttons
- Cancel();
- }
- catch(SQLException error)
- {
- showStatus("Error: " + error.toString());
- }
- }
- }
- public void Delete()
- {
- //Delete the current record
- int intIndex = lstNames.getSelectedIndex();
- String strCustomerID = lstNames.getSelectedItem();
- if(intIndex == 0) //Make sure a record is selected
- //Position zero holds a message
- showStatus("Please select the record to be deleted");
- else
- {
- try
- {
- //Delete from Database
- Statement cmdCustomers = conCustomers.createStatement();
- cmdCustomers.executeUpdate("Delete from Customers Where CustomerID = '"+strCustomerID+"'");
- Statement cmdCustomers2 = conCustomers.createStatement();
- cmdCustomers2.executeUpdate("Delete from Orders Where ID = '"+strCustomerID+"'");
- ClearTextFields(); //Delete from screen
- lstNames.remove(intIndex); //Delete from list
- showStatus("Record deleted"); //Display message
- }
- catch(SQLException error) //Exception Handling
- {
- showStatus("Error during Delete"); //Displayed if record can not be deleted
- }
- }
- }
- public void Cancel()
- {
- //Enable the Delete and Edit buttons
- btnDelete.setEnabled(true);
- btnEdit.setEnabled(true);
- btnCancel.setEnabled(false);
- //Change caption of button
- btnAdd.setLabel("Add");
- //Clear the text fields and status bar
- ClearTextFields();
- showStatus("");
- }
- public void Edit() {
- // Save the modified record
- String strCustomerID = lstNames.getSelectedItem();
- String updateLine = null;
- int intIndex = lstNames.getSelectedIndex();
- if (intIndex == 0) // Make sure a record is selected
- // Position zero holds a message
- showStatus("Please select the record to be changed");
- else {
- try {
- Statement cmdCustomers = conCustomers.createStatement();
- updateLine = String.format("UPDATE Customers SET CustomerName = '%s', PhoneNumber = '%s', Address = '%s', City = '%s', State = '%s', Zip = '%s' WHERE CustomerID = '%s'",
- txtCustomerName.getText(),
- txtPhone.getText(),
- txtAddress.getText(),
- txtCity.getText(),
- txtState.getText(),
- txtZip.getText(),
- strCustomerID);
- cmdCustomers.executeUpdate(updateLine);
- String updateLine2 = String.format("UPDATE Orders SET ProductName = '%s', OrderNumber = '%s', Quantity = '%s', UnitCost = '%s' WHERE ID = '%s'",
- txtProductName.getText(),
- txtOrderNumber.getText(),
- txtQuantity.getText(),
- txtUnitCost.getText(),
- strCustomerID);
- Statement cmdCustomers2 = conCustomers.createStatement();
- cmdCustomers2.executeUpdate(updateLine2);
- if (!strCustomerID.equals(txtCustomerID.getText())) {
- // Last name changed; change the list
- lstNames.remove(intIndex); // Remove the old entry
- lstNames.add(txtCustomerID.getText()); // Add the new entry
- }
- } catch (SQLException error) {
- showStatus(updateLine); //showStatus("Error: " + error.toString());
- }
- }
- }
- public void firstRecord() //Navigates to the first record in the listbox
- {
- String strCustomerID = lstNames.getSelectedItem();
- int first = 1;
- lstNames.select(first);
- try
- {
- cmdCustomers = conCustomers.createStatement();
- rsCustomers = cmdCustomers.executeQuery( "Select * From Customers Where CustomerID = \'"+strCustomerID+"\'");
- txtCustomerID.setText(strCustomerID);
- displayRecord(rsCustomers);
- }
- catch(SQLException error) //Exception Handling
- {
- showStatus("item state try.");
- }
- }
- public void previousRecord() //Navigates backwards through the records in the listbox
- {
- String strCustomerID = lstNames.getSelectedItem();
- int PreviousIndex = 0;
- PreviousIndex = lstNames.getSelectedIndex();
- PreviousIndex--;
- if (PreviousIndex > 0)
- {
- lstNames.select(PreviousIndex);
- try
- {
- Statement cmdCustomers = conCustomers.createStatement();
- rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
- txtCustomerID.setText(strCustomerID);
- displayRecord(rsCustomers);
- }
- catch(SQLException error)
- {
- showStatus("item state try.");
- }
- }
- else
- {
- showStatus("You've reached the beginning of the list.");
- }
- }
- public void nextRecord() //Navigates to the next record in the listbox
- {
- String strCustomerID = lstNames.getSelectedItem();
- int NextIndex = 0;
- NextIndex = lstNames.getSelectedIndex();
- if(NextIndex == 0)
- {
- NextIndex = 1;
- }
- else
- {
- NextIndex++;
- }
- if (NextIndex < lstNames.getItemCount())
- {
- lstNames.select(NextIndex);
- try
- {
- Statement cmdCustomers = conCustomers.createStatement();
- rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
- txtCustomerID.setText(strCustomerID);
- displayRecord(rsCustomers);
- }
- catch(SQLException error)
- {
- showStatus("item state try.");
- }
- }
- else
- {
- showStatus("You've reached the end of the list.");
- }
- }
- public void lastRecord() //Navigates to the last record held in the listbox
- {
- String strCustomerID = lstNames.getSelectedItem();
- int last = 0;
- while (last < lstNames.getItemCount()-1)
- {
- last++;
- }
- lstNames.select(last);
- try
- {
- Statement cmdCustomers = conCustomers.createStatement();
- rsCustomers = cmdCustomers.executeQuery( "Select * From Orders, Customers Where Orders.ID = \'"+strCustomerID+"\' and Customers.CustomerID = \'"+strCustomerID+"\'");
- txtCustomerID.setText(strCustomerID);
- displayRecord(rsCustomers);
- }
- catch(SQLException error)
- {
- showStatus("item state try.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement