0% found this document useful (0 votes)
1 views41 pages

AddressBook1

The document is a Java program for an Address Book application created by Mr. Jake Rodriguez Pomperada. It includes functionalities for adding, editing, deleting, and searching records, as well as displaying an about dialog. The program utilizes JDBC for database connectivity and features a graphical user interface built with AWT and Swing components.

Uploaded by

prdwivedi707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views41 pages

AddressBook1

The document is a Java program for an Address Book application created by Mr. Jake Rodriguez Pomperada. It includes functionalities for adding, editing, deleting, and searching records, as well as displaying an about dialog. The program utilizes JDBC for database connectivity and features a graphical user interface built with AWT and Swing components.

Uploaded by

prdwivedi707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

+/* AddressBook.

java

* Author : Mr. Jake Rodriguez Pomperada, MAED-Instructional Technology

* Date : February 13, 2009 8:10 AM

* Email : [email protected]

* */

////////////////////////////////////////////////////////////////////////////////

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.applet.*;

import javax.swing.*;

///////////////////////////////////////////////////////////////////////////////

//Creates the main window with top Menu toolbar.

//This is also the class for the main declaration

//////////////////////////////////////////////////////////////////////////////

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("Address Book Version 1.0");

appBook.setSize(400, 300);

appBook.setBackground(Color.WHITE);

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(520,100);

setFont(new Font("Arial", Font.BOLD, 14));

setLocation(70,30);

//Creates a Label and a Button for the dialog

Label company = new Label("Address Book Created by Mr. Jake R. Rodriguez Pomperada,MAED-
IT");

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 Name1,Surname1,Address1,Phone1;

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(5,3,10,20));

setSize(300,350);

setResizable(true);

//Creates new Labels to describe the text boxes.

Label Name,Surname,Address,Phone;

Name = new Label("Name: ");

Surname = new Label("Surname: ");

Address = new Label("Address: ");

Phone = new Label("Phone Number: ");

//Creates TextBoxes for the user to enter data into

Name1 = new TextField(10);

Surname1 = new TextField(10);

Address1 = new TextField(20);

Phone1 = 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;

space1 = new Label(" ");

space2 = new Label(" ");

space3 = new Label(" ");

space4 = new Label(" ");

space5 = new Label(" ");

//Adds new Labels, Textboxes and Button

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

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////

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 Name = Name1.getText();

String SName = Surname1.getText();

String Address = Address1.getText();

String Phone = Phone1.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)VALUES('" +Name+


"','"+SName+"','"+Address+"','"+Phone+"')";

//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;

//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(5,3,10,20));

setSize(300, 250);

setResizable(false);

//Sets the labels that goes onto the Dialog box

Label Name, Surname, Address, Phone;


Name = new Label("Name: ");

Surname = new Label("Surname: ");

Address = new Label ("Address: ");

Phone = new Label("Phone: ");

//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);

//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);
//Creates space labels to fill up the space in the GridLayout

Label space1, space2, space3, space4, space5;

space1 = new Label(" ");

space2 = new Label(" ");

space3 = new Label(" ");

space4 = new Label(" ");

space5 = new Label(" ");

//Add all the controls to the Dialog.

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();

//Handles all the events happeing on the dialog box

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();

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

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////

public void itemStateChanged(ItemEvent ie) {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";


//This will hold the currently selected name in the list in myChoice

String myChoice = NameList.getSelectedItem();

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);

Statement stmt = null;

Connection con = null;

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.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////

public void GetData() {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";

String SQL = "SELECT name FROM Address" ;


try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);

Statement stmt = null;

Connection con = null;

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

//entered into the database.

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

//list has been populated

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.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////

public void DataUpdate() {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";

//Loads the database driver

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);

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);

//These String variables will hold the updated data retrieved from the TextFields.

String upSurname = SurnameField.getText();

String upAddress = AddressField.getText();

String upPhone = PhoneField.getText();

//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()+"'";

//Communicates with database


try {

stmt.executeUpdate(SQL);

con.close();

dispose();

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);

////////////////////////////////////////////////////////////////////////////////

//This class will delete data from the database

///////////////////////////////////////////////////////////////////////////////

class DelRec extends Dialog implements ActionListener, ItemListener {

//Declaration of TextFields

TextField SurnameField, AddressField, PhoneField;

//Declaration of drop down list

Choice NameList;

public DelRec (AddressBook parent, String title) {

super (parent, title, true);


setLayout(new GridLayout(5,3,10,20));

setSize(300, 250);

setResizable(false);

//Declaration of labels that will describe the TextFields

Label Name, Surname, Address, Phone;

//Creates the Labels

Name = new Label("Name");

Surname = new Label("Surname");

Address = new Label("Address");

Phone = new Label("Phone");

//Declaration of labels that will be used as spacers in the GridLayout

Label space1, space2, space3, space4, space5;

//Creates the spacer labels.

space1 = new Label(" ");

space2 = new Label(" ");

space3 = new Label(" ");

space4 = new Label(" ");

space5 = new Label(" ");

//Creates the button on the dialog

Button BtnDel = new Button("Delete");


Button BtnCn = new Button("Cancel");

//Creates the drop down list

NameList = new Choice();

//Adds listeners to buttons and drop down list

BtnDel.addActionListener(this);

BtnCn.addActionListener(this);

NameList.addItemListener(this);

//Creates the TextFields

SurnameField = new TextField(10);

AddressField = new TextField(20);

PhoneField = new TextField(10);

//Adds the components to the dialog

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.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();

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) {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";

//This will hold the currently selected name in the list in myChoice

String myChoice = NameList.getSelectedItem();

//This is the SQL query for extracting data from the database.

String SQL = "SELECT name,surname,address,phone FROM Address WHERE name ='" +myChoice+
"'";

//Loads the driver for communicating with database

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception ex) {

JOptionPane.showMessageDialog(null,""+ex.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);

Statement stmt = null;

Connection con = null;

//Creates a connection to the database


try {

con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser,dbpasswd);

stmt = con.createStatement();

} catch (Exception ex) {

JOptionPane.showMessageDialog(null,""+ex.getMessage(),"Connection
Error",JOptionPane.WARNING_MESSAGE);

ResultSet rs = null;

//Executes the Sl query on the database.

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 ex) {

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() {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";

String SQL = "SELECT name FROM address";

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver
Error",JOptionPane.WARNING_MESSAGE);

Statement stmt = null;

Connection con = null;

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);

while (rs.next()) {

NameList.add(rs.getString("name"));

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Problem",JOptionPane.WARNING_MESSAGE);

//This method wil delete the currently selected record.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////

public void DelCurRec() {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";


//MyChoice will hold the value for the currently selected item

String myChoice = NameList.getSelectedItem();

//This is the SQL string

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);

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);

//Execute the SQL statment for deleting records


try {

stmt.executeUpdate(SQL);

//This closes the connection to the database

con.close();

//This closes the dialog

dispose();

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"Communication
Error",JOptionPane.WARNING_MESSAGE);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////

//This class will create the Search Dialog.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////

class SearchDlg extends Dialog implements ActionListener, ItemListener {

//This declares a TextField that will be put on the dialog

TextField Name;

//This creates a Search and Cancel button on the dialog

Button BtnGo, BtnCn;


//This creates checkboxes for different search queries

Checkbox option1 = new Checkbox("By Name");

Checkbox option2 = new Checkbox("By Surname");

public SearchDlg (AddressBook parent, String title) {

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

Label Srch = new Label("Search For :");

//This creates a Textfield for the user input

Name = new TextField(10);

//Creates button for Search and Cancel on the dialog

BtnGo = new Button("Search");

BtnCn = new Button("Cancel");

//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);

//Adds Item Listeners to the checkboxes

option1.addItemListener(this);

option2.addItemListener(this);

//This will create spacer labels for the GridLayout

Label space1, space2, space3;

space1 = new Label(" ");

space2 = new Label(" ");

space3 = new Label(" ");

//Add Controls to the dialog.

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

//////////////////////////////////////////////////////////////////////////////////////////////////////////

public void actionPerformed(ActionEvent e) {

String str = e.getActionCommand();

if (str.equals("Search")) {

GoSrch();

if (str.equals("Cancel")) {

dispose();

//This will handle the events from clicking the ChecBoxes on the dialog

//the Search button will also be enable when a selection is made.

/////////////////////////////////////////////////////////////////////////////////////////////////////////

public void itemStateChanged(ItemEvent e) {

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

/////////////////////////////////////////////////////////////////////////////////////////////////////////

public void GoSrch() {

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
/////////////////////////////////////////////////////////////////////////////////////////////////////////

public void Srch1() {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";

String mySearch = Name.getText();

//This is the SQL String for retrieving data by name from the database.

String SQL = "SELECT name,surname,address,phone FROM Address WHERE name = '"


+mySearch+"'";

//This loads the driver for the database

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"Database Driver Error",


JOptionPane.WARNING_MESSAGE);

Statement stmt = null;

Connection con = null;


//Creates connection to the database.

try {

con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser, dbpasswd);

stmt = con.createStatement();

} catch (Exception e) {

JOptionPane.showMessageDialog(null, " "+e.getMessage(),"Cannot Connect to


Database",JOptionPane.WARNING_MESSAGE);

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);

//Makes use of a swing OptionPane to display information of the successful search.

JOptionPane.showMessageDialog(null,Result,"Record Found",
JOptionPane.INFORMATION_MESSAGE);

//Close the connection to the database.

con.close();

this.dispose();

} catch (Exception e) {
//Makes use of the JFC Swing OptionPane to display error message

JOptionPane.showMessageDialog(null ,"Record not Found","Warning",


JOptionPane.INFORMATION_MESSAGE);

//This method will search the database for a input record according to the surname.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////

public void Srch2() {

String dbuser = " ";

String dbpasswd = " ";

String DriverPrefix = "jdbc:odbc:";

String DataSource = "AddressBook";

String mySearch = Name.getText();

//This is the SQL String for retrieving data by name from the database.

String SQL = "SELECT name,surname,address,phone FROM Address WHERE surname = '"


+mySearch+"'";

//This loads the driver for the database

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {

JOptionPane.showMessageDialog(null,""+e.getMessage(),"Database Driver Error",


JOptionPane.WARNING_MESSAGE);

Statement stmt = null;

Connection con = null;

//Creates connection to the database.

try {

con = DriverManager.getConnection(DriverPrefix+DataSource,dbuser, dbpasswd);

stmt = con.createStatement();

} catch (Exception e) {

JOptionPane.showMessageDialog(null, " "+e.getMessage(),"Cannot Connect to


Database",JOptionPane.WARNING_MESSAGE);

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);

//Makes use of a swing OptionPane to display information of the successful search.


JOptionPane.showMessageDialog(null,Result,"Record Found",
JOptionPane.INFORMATION_MESSAGE);

//Close the connection to the database.

con.close();

this.dispose();

} catch (Exception e) {

//Makes use of the JFC Swing OptionPane to display error message

JOptionPane.showMessageDialog(null ,"Record not Found","Warning",


JOptionPane.INFORMATION_MESSAGE);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////

//This class handles the menubar events

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////

class myMenuHandler implements ActionListener {

AddressBook appbook;

public myMenuHandler(AddressBook appbook) {

this.appbook = appbook;

}
//This code will display an Dialog Boxes for the different Menu Selections

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////

public void actionPerformed(ActionEvent ae) {

String arg = (String)ae.getActionCommand();

//This code executed when Exit is selected on the Menu Bar

if (arg.equals("Exit")) {

appbook.dispose();

//This will start the creation of the Add Dialog

if (arg.equals("Add...")) {

AddDlg Adlg = new AddDlg(appbook, "Add New Address");

Adlg.setVisible(true);

//This will start the creation of the Edit Dialog

if (arg.equals("Edit...")) {

EditDlg Edlg = new EditDlg(appbook, "Edit Records");

Edlg.setVisible(true);

//This will start the creation of the Delete Dialog

if (arg.equals("Delete...")) {
DelRec dlg = new DelRec(appbook, "Delete Records");

dlg.setVisible(true);

//This will start the creation of the Search Dialog

if (arg.equals("Search...")) {

SearchDlg schDlg = new SearchDlg(appbook, "Search Records");

schDlg.setVisible(true);

//This will Display the About Dialog

if (arg.equals("About Address Book...")) {

AboutDlg d = new AboutDlg(appbook, "About");

d.setVisible(true);

} // End of Code

////////////////////////////////////////////////////////////////////////////////////////////////////

You might also like