AddressBook1
AddressBook1
java
* Email : [email protected]
* */
////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.applet.*;
import javax.swing.*;
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
public AddressBook() {
initComponents();
}
public void initComponents() {
setMenuBar(menubar);
MenuItem item1;
menubar.add(file);
MenuItem item2,item3,item4,item5;
menubar.add(names);
MenuItem item6;
addWindowListener(adapter);
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
//Variable decelaration
///////////////////////////////////////////////////////////////////////////////
//Creates main window, sets Title, Height and Width and Visibility
appBook.setSize(400, 300);
appBook.setBackground(Color.WHITE);
appBook.setVisible(true);
///////////////////////////////////////////////////////////////////////////////
//This Class handles the event for closing the main program window
///////////////////////////////////////////////////////////////////////////////
AddressBook appbook;
this.appbook = appbook;
appbook.setVisible(false);
appbook.dispose();
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
setLayout(new FlowLayout(FlowLayout.CENTER));
setSize(520,100);
setLocation(70,30);
Label company = new Label("Address Book Created by Mr. Jake R. Rodriguez Pomperada,MAED-
IT");
close.addActionListener(this);
add(company);
add(close);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/
if(str.equals("Close")) {
dispose();
///////////////////////////////////////////////////////////////////////////////
//This creates the Add New Address Dialog for entering new data into the
//database.
//////////////////////////////////////////////////////////////////////////////
TextField Name1,Surname1,Address1,Phone1;
Button BtnOK,BtnCn;
setLayout(new GridLayout(5,3,10,20));
setSize(300,350);
setResizable(true);
Label Name,Surname,Address,Phone;
BtnOK.addActionListener(this);
BtnCn.addActionListener(this);
Label space1,space2,space3,space4,space5;
add(Name);
add(Name1);
add(space1);
add(Surname);
add(Surname1);
add(space2);
add(Address);
add(Address1);
add(space3);
add(Phone);
add(Phone1);
add(space4);
add(space5);
add(BtnOK);
add(BtnCn);
//This method hales all the events that take place on the dialog
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////
if (str.equals("OK")){
ToDataBase();
if (str.equals("Cancel")) {
dispose();
//Open a connection to the AddressBook database and update it with the data
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////
public void ToDataBase() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(Exception e) {
//Uses the JFC Swing to display warning message in Option Pane with
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
return;
try {
con = DriverManager.getConnection(DriverPrefix+DataSource,
dbuser, dbpasswd);
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
try {
stmt.executeUpdate(SQLString);
con.close();
this.dispose();
}catch (Exception e) {
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Choice NameList;
setSize(300, 250);
setResizable(false);
//Adds a pop up list to the dialog with names from the database
BtnUpdate.addActionListener(this);
BtnCn.addActionListener(this);
NameList.addItemListener(this);
//Creates space labels to fill up the space in the GridLayout
add(Name);
add(NameList);
add(space1);
add(Surname);
add(SurnameField);
add(space2);
add(Address);
add(AddressField);
add(space3);
add(Phone);
add(PhoneField);
add(space4);
add(space5);
add(BtnUpdate);
add(BtnCn);
GetData();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
if (str.equals("Update")){
DataUpdate();
if (str.equals("Cancel")) {
dispose();
//This method handles the Event action that take place on the Choide Drop Down List
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////
String SQL = "SELECT name,surname,address,phone FROM Address WHERE name ='" +myChoice+
"'";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
try {
con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
ResultSet rs = null;
try {
rs = stmt.executeQuery(SQL);
rs.next();
SurnameField.setText(rs.getString(2));
AddressField.setText(rs.getString(3));
PhoneField.setText(rs.getString(4));
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);
//This method will populate the pop up list with names from the database.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
try {
con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
ResultSet rs = null;
try {
rs = stmt.executeQuery(SQL);
//This will populate the drop down list with all the names of people
while (rs.next()) {
NameList.add(rs.getString("name"));
} catch(Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);
//This try - catch sequence will close the database after the drop down
try {
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Warning",JOptionPane.WARNING_MESSAGE)
;
//This will update the change data from the TextFields to the database.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
try {
con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
//These String variables will hold the updated data retrieved from the TextFields.
//This is the SQL String that updates the data from the TextFields to the database
String SQL = "UPDATE Address SET surname = '"+upSurname+"', address = '"+upAddress+"', phone =
'"+upPhone+"' WHERE name = '"+NameList.getSelectedItem()+"'";
stmt.executeUpdate(SQL);
con.close();
dispose();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Declaration of TextFields
Choice NameList;
setSize(300, 250);
setResizable(false);
BtnDel.addActionListener(this);
BtnCn.addActionListener(this);
NameList.addItemListener(this);
add(Name);
add(NameList);
add(space1);
add(Surname);
add(SurnameField);
add(space2);
add(Address);
add(AddressField);
add(space3);
add(Phone);
add(PhoneField);
add(space4);
add(space5);
add(BtnDel);
add(BtnCn);
GetData();
//This method handles all the action events from the Delete and Cancel buttons.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////
if (str.equals("Delete")) {
DelCurRec();
if (str.equals("Cancel")) {
dispose();
//Updates the TextFields with the rest of the data when selection is made.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////
public void itemStateChanged(ItemEvent e) {
//This will hold the currently selected name in the list in myChoice
//This is the SQL query for extracting data from the database.
String SQL = "SELECT name,surname,address,phone FROM Address WHERE name ='" +myChoice+
"'";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JOptionPane.showMessageDialog(null,""+ex.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
stmt = con.createStatement();
JOptionPane.showMessageDialog(null,""+ex.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
ResultSet rs = null;
try {
rs = stmt.executeQuery(SQL);
rs.next();
SurnameField.setText(rs.getString(2));
AddressField.setText(rs.getString(3));
PhoneField.setText(rs.getString(4));
con.close();
JOptionPane.showMessageDialog(null,""+ex.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);
//This method will populate the drop downlist with data from the database.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////
public void GetData() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
try {
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
}
ResultSet rs = null;
try {
rs = stmt.executeQuery(SQL);
while (rs.next()) {
NameList.add(rs.getString("name"));
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Problem",JOptionPane.WARNING_MESSAGE);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
String SQL = "DELETE FROM Address WHERE name = '" +myChoice+ "'";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);
try {
con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);
stmt = con.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);
stmt.executeUpdate(SQL);
con.close();
dispose();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////
TextField Name;
super(parent,title,false);
setSize(300, 100);
setLayout(new GridLayout(3,2,8,5));
setResizable(false);
setLocation(300,50);
//This creates a label for the search dialog describing the TextField
//Disables the Search button unitl a selection is made from the CheckBoxes
BtnGo.setEnabled(false);
//Adds event listeners to the Button.
BtnGo.addActionListener(this);
BtnCn.addActionListener(this);
option1.addItemListener(this);
option2.addItemListener(this);
add(Srch);
add(Name);
add(space1);
add(option1);
add(option2);
add(space2);
add(space3);
add(BtnGo);
add(BtnCn);
}
//This method handles all the events that take place on the dialog
//////////////////////////////////////////////////////////////////////////////////////////////////////////
if (str.equals("Search")) {
GoSrch();
if (str.equals("Cancel")) {
dispose();
//This will handle the events from clicking the ChecBoxes on the dialog
/////////////////////////////////////////////////////////////////////////////////////////////////////////
if (option1.getState() == true) {
option2.setEnabled(false);
BtnGo.setEnabled(true);
} else {
option2.setEnabled(true);
BtnGo.setEnabled(false);
if (option2.getState() == true) {
option1.setEnabled(false);
BtnGo.setEnabled(true);
} else {
option1.setEnabled(true);
//This method will search for the selected record in the database
/////////////////////////////////////////////////////////////////////////////////////////////////////////
if (option1.getState() == true) {
Srch1();
if (option2.getState() == true) {
Srch2();
//This method will search the database by the name that is input into the TextField
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//This is the SQL String for retrieving data by name from the database.
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
try {
stmt = con.createStatement();
} catch (Exception e) {
ResultSet rs = null;
//Executes the SQL query on the database and displays the result in a JFC OptionPane
try {
rs = stmt.executeQuery(SQL);
rs.next();
String Result = rs.getString(1) + " " + rs.getString(2) + "\n" + rs.getString(3) + "\n" + rs.getString(4);
JOptionPane.showMessageDialog(null,Result,"Record Found",
JOptionPane.INFORMATION_MESSAGE);
con.close();
this.dispose();
} catch (Exception e) {
//Makes use of the JFC Swing OptionPane to display error message
//This method will search the database for a input record according to the surname.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////
//This is the SQL String for retrieving data by name from the database.
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
try {
stmt = con.createStatement();
} catch (Exception e) {
ResultSet rs = null;
//Executes the SQL query on the database and displays the result in a JFC OptionPane
try {
rs = stmt.executeQuery(SQL);
rs.next();
String Result = rs.getString(1) + " " + rs.getString(2) + "\n" + rs.getString(3) + "\n" + rs.getString(4);
con.close();
this.dispose();
} catch (Exception e) {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////
AddressBook appbook;
this.appbook = appbook;
}
//This code will display an Dialog Boxes for the different Menu Selections
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////
if (arg.equals("Exit")) {
appbook.dispose();
if (arg.equals("Add...")) {
Adlg.setVisible(true);
if (arg.equals("Edit...")) {
Edlg.setVisible(true);
if (arg.equals("Delete...")) {
DelRec dlg = new DelRec(appbook, "Delete Records");
dlg.setVisible(true);
if (arg.equals("Search...")) {
schDlg.setVisible(true);
d.setVisible(true);
} // End of Code
////////////////////////////////////////////////////////////////////////////////////////////////////