📄 addressbook.java
字号:
/**
* Nikunj Parikh December 2, 2006
* Assignment Java Database Program
*/
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.applet.*;
import javax.swing.*;
public class AddressBook extends Frame {
public AddressBook() {
initComponents();
}
public void initComponents() {
//Creates menu bar and add it to frame
menubar = new MenuBar();
setMenuBar(menubar);
//Create File menu with Items
Menu file = new Menu("File");
MenuItem item1;
file.add(item1 = new MenuItem("Exit"));
menubar.add(file);
//Creates Records menu with items
Menu names = new Menu("Records");
MenuItem item2,item3,item4,item5;
names.add(item2 = new MenuItem("Add..."));
names.add(item3 = new MenuItem("Edit..."));
names.add(item4 = new MenuItem("Delete..."));
names.add(item5 = new MenuItem("Search..."));
menubar.add(names);
//Creates About menu Item
Menu about = new Menu("About");
MenuItem item6;
about.add(item6 = new MenuItem("About Address Book..."));
menubar.add(about);
//Create an object to handle window events
myWindowAdapter adapter = new myWindowAdapter(this);
addWindowListener(adapter);
//Create an object to handle action and item events
myMenuHandler handler = new myMenuHandler(this);
//register it to receive events
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
}
//Variable decelaration
private Menu menu;
private MenuBar menubar;
///////////////////////////////////////////////////////////////////////////////
// This is the main declaration
//////////////////////////////////////////////////////////////////////////////
public static void main(String args[]){
//Creates main window, sets Title, Height and Width and Visibility
AddressBook appBook = new AddressBook();
appBook.setTitle("Employee Data");
appBook.setSize(500, 300);
appBook.setBackground(Color.BLUE);
appBook.setVisible(true);
}
}
///////////////////////////////////////////////////////////////////////////////
//This Class handles the event for closing the main program window
///////////////////////////////////////////////////////////////////////////////
class myWindowAdapter extends WindowAdapter {
AddressBook appbook;
public myWindowAdapter(AddressBook appbook) {
this.appbook = appbook;
}
public void windowClosing(WindowEvent we) {
appbook.setVisible(false);
appbook.dispose();
}
}
///////////////////////////////////////////////////////////////////////////////
//This class creates the About Dialog
///////////////////////////////////////////////////////////////////////////////
class AboutDlg extends Dialog implements ActionListener {
AboutDlg(AddressBook parent,String title) {
super (parent, title, true);
setLayout(new FlowLayout(FlowLayout.CENTER));
setSize(300,100);
setFont(new Font("Arial", Font.BOLD, 12));
setLocation(70,30);
//Creates a Label and a Button for the dialog
Label company = new Label("Address Book Created by Nikunj Parikh.");
Button close = new Button("Close");
//Adds Action Listener to Close Button
close.addActionListener(this);
//Adds Labels and Button to the dialog .
add(company);
add(close);
}
//Handles the actionevents performed on the dialog to close the dialog.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(str.equals("Close")) {
dispose();
}
}
}
///////////////////////////////////////////////////////////////////////////////
//This creates the Add New Address Dialog for entering new data into the
//database.
//////////////////////////////////////////////////////////////////////////////
class AddDlg extends Dialog implements ActionListener {
TextField EmployeeID1,Name1,Surname1,Address1,Phone1,City1,State1, Zip1,
OfficePhoneNumber1, DepartmentAssg1,YearsEmployee1, MonthlyPay1;
Button BtnOK,BtnCn;
public AddDlg(AddressBook parent, String title) {
//Sets the way that the Dialog will look on screen
super(parent, title, true);
setLayout(new GridLayout(13,10,20,10));
setSize(300,400);
setResizable(false);
//Creates new Labels to describe the text boxes.
Label EmployeeID,Name,Surname,Address,Phone,City,State, Zip,
OfficePhoneNumber, DepartmentAssg,YearsEmployee, MonthlyPay;
EmployeeID = new Label("Employee ID: ");
Name = new Label("Name: ");
Surname = new Label("Surname: ");
Address = new Label("Address: ");
City = new Label("City: ");
State = new Label("State: ");
Zip = new Label ("Zip: ");
Phone = new Label("Home Phone Number: ");
OfficePhoneNumber = new Label ("Office Phone Number: ");
DepartmentAssg = new Label ("Department Assigned: ");
YearsEmployee = new Label ("Years Employeed: ");
MonthlyPay = new Label ("Monthly Pay: ");
//Creates TextBoxes for the user to enter data into
EmployeeID1 = new TextField(10);
Name1 = new TextField(10);
Surname1 = new TextField(10);
Address1 = new TextField(20);
Phone1 = new TextField(10);
City1 = new TextField(10);
State1 = new TextField(10);
Zip1 = new TextField(10);
OfficePhoneNumber1 = new TextField(10);
DepartmentAssg1 = new TextField(10);
YearsEmployee1 = new TextField(10);
MonthlyPay1 = new TextField(10);
BtnOK = new Button("OK");
BtnCn = new Button("Cancel");
//Creates Listeners for Buttons
BtnOK.addActionListener(this);
BtnCn.addActionListener(this);
//Creates spacer labels for the GridLayout
Label space1,space2,space3,space4,space5,
space6,space7,space8,space9,space10,space11,space12;
space1 = new Label(" ");
space2 = new Label(" ");
space3 = new Label(" ");
space4 = new Label(" ");
space5 = new Label(" ");
space6 = new Label(" ");
space7 = new Label(" ");
space8 = new Label(" ");
space9 = new Label(" ");
space10 = new Label(" ");
space11= new Label(" ");
space12 = new Label(" ");
//Adds new Labels, Textboxes and Button
add(EmployeeID);
add(EmployeeID1);
add(space12);
add(Name);
add(Name1);
add(space1);
add(Surname);
add(Surname1);
add(space2);
add(Address);
add(Address1);
add(space3);
add(City);
add(City1);
add(space4);
add(State);
add(State1);
add(space5);
add(Zip);
add(Zip1);
add(space6);
add(Phone);
add(Phone1);
add(space7);
add(OfficePhoneNumber);
add(OfficePhoneNumber1);
add(space8);
add(DepartmentAssg);
add(DepartmentAssg1);
add(space9);
add(YearsEmployee);
add(YearsEmployee1);
add(space10);
add(MonthlyPay);
add(MonthlyPay1);
add(space11);
add(BtnOK);
add(BtnCn);
}
//This method hales all the events that take place on the dialog
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("OK")){
ToDataBase();
}
if (str.equals("Cancel")) {
dispose();
}
}
//Open a connection to the AddressBook database and update it with the data
//that the user has entered into the textfields.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void ToDataBase() {
//Retrieve info from the Textfields in die Dialog.
String EmployeeID = EmployeeID1.getText();
String Name = Name1.getText();
String SName = Surname1.getText();
String Address = Address1.getText();
String City = City1.getText();
String State = State1.getText();
String Zip = Zip1.getText();
String Phone = Phone1.getText();
String OfficePhoneNumber = OfficePhoneNumber1.getText();
String DepartmentAssg = DepartmentAssg1.getText();
String YearsEmployee = YearsEmployee1.getText();
String MonthlyPay = MonthlyPay1.getText();
//Parameters for the creation of the database.
String dbuser = "";
String dbpasswd = "";
String DriverPrefix = "jdbc:odbc:";
String DataSource ="AddressBook";
//The SQL String
String SQLString = "INSERT INTO Address(name,surname,address,phone,city,state,zip,officephonenumber,deaprtmentassg,yearsemployee,monthlypay)VALUES('" +Name+ "','"+SName+"','"+Address+"','"+Phone+"','"+City+"','"+State+"','"+Zip+"','"+OfficePhoneNumber+"','"+DepartmentAssg+"','"+YearsEmployee+"','"+MonthlyPay+"')";
//Loads JDBC/ODBC driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(Exception e) {
//Uses the JFC Swing to display warning message in Option Pane with
//relevant information about the error.
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver Error",JOptionPane.WARNING_MESSAGE);
return;
}
Statement stmt = null;
Connection con = null;
//Creates connection to database
try {
con = DriverManager.getConnection(DriverPrefix+DataSource,
dbuser, dbpasswd);
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection Error",JOptionPane.WARNING_MESSAGE);
}
//Updates the database with data
try {
stmt.executeUpdate(SQLString);
con.close();
this.dispose();
}catch (Exception e) {
JOptionPane.showMessageDialog(null,"Check that all TextFields have been completed.\n"+e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
}
}
}
///////////////////////////////////////////////////////////////////////////////
//This class will create a Dialog fo the editing of existing records.
///////////////////////////////////////////////////////////////////////////////
class EditDlg extends Dialog implements ActionListener, ItemListener {
//Sets the textfields that goes to the Dialog box
TextField SurnameField, AddressField, PhoneField, CityField, StateField,
ZipField, OfficePhoneNumberField, DepartmentAssgField, YearsEmployeeField,
MonthlyPayField;
//Creates the pop uplist on the dialog
Choice NameList;
public EditDlg(AddressBook parent, String title) {
//Sets the dimensions for the Edit dialog box.
super(parent, title, true);
setLayout (new GridLayout(13,10,20,10));
setSize(300, 400);
setResizable(false);
//Sets the labels that goes onto the Dialog box
Label Name, Surname, Address, Phone, City, State, Zip,
OfficePhoneNumber, DepartmentAssg,YearsEmployee, MonthlyPay;;
Name = new Label("Name: ");
Surname = new Label("Surname: ");
Address = new Label ("Address: ");
City = new Label("City: ");
State = new Label("State: ");
Zip = new Label ("Zip: ");
Phone = new Label("Home Phone Number: ");
OfficePhoneNumber = new Label ("Office Phone Number: ");
DepartmentAssg = new Label ("Department Assigned: ");
YearsEmployee = new Label ("Years Employeed: ");
MonthlyPay = new Label ("Monthly Pay: ");
//Adds a pop up list to the dialog with names from the database
NameList = new Choice();
//Sets the sizes of the textfields
SurnameField = new TextField(10);
AddressField = new TextField(20);
PhoneField = new TextField(10);
CityField = new TextField(10);
StateField = new TextField(10);
ZipField = new TextField(10);
OfficePhoneNumberField = new TextField(10);
DepartmentAssgField = new TextField(10);
YearsEmployeeField = new TextField(10);
MonthlyPayField = new TextField (10);
//Creates the buttons on the dialog box
Button BtnUpdate, BtnCn;
BtnUpdate = new Button("Update");
BtnCn = new Button("Cancel");
//Adds listeners to the buttons
BtnUpdate.addActionListener(this);
BtnCn.addActionListener(this);
NameList.addItemListener(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -