Question: For this assignment you will be modifying the Phone Book application provided in Moodle and recreating it to be an AddressBook application. Use the CreatePhoneBookDB

For this assignment you will be modifying the Phone Book application provided in Moodle and recreating it to be an AddressBook application.

Use the CreatePhoneBookDB class provided in Moodle and add some data to the table to create an address book. Make sure to create a primary key that is truly unique, include first and last name, phone number, street address, and email address. Modify the code to add these fields to the table, then be sure to add data to the insert statements. Run this to create the AddressBookDB.

Modify the PhoneBookManager and PhoneBookDemo programs to become an address book application and display the first and last name of each person in the address book for users to choose from. When the user views a person be sure to display all information associated with that record. Allow the user to edit each field, not just the phone number. The user should be able to update anything tied to the person in the address book with the exception of the primary key. Make sure when a new person is added to the address book all information is asked for and recorded. Keep the functionality that allows the user to delete a record.

These are the minimum requirements for this assignment.

***********************************************************************************************************************************

import java.sql.*;

/** This program creates the PhoneBook database for the PhoneBook Database programming challenge. */ public class CreatePhoneBookDatabase { public static void main(String[] args) throws Exception { String sql; final String DB_URL = "jdbc:derby:PhoneBook;create=true"; try { // Create a connection to the database. Connection conn = DriverManager.getConnection(DB_URL); // Create a Statement object. Statement stmt = conn.createStatement(); // Create the Entries table. System.out.println("Creating the Entries table..."); stmt.execute("CREATE TABLE Entries" + "(Name CHAR(25) NOT NULL PRIMARY KEY, " + "PhoneNumber CHAR(15))"); // Add some rows to the new table. sql = "INSERT INTO Entries VALUES" + "('Jean', '555-2222')"; stmt.executeUpdate(sql);

stmt.executeUpdate(sql);

// Close the resources. stmt.close(); conn.close(); System.out.println("Done"); } catch(Exception ex) { System.out.println("ERROR: " + ex.getMessage()); } } }

***********************************************************************************************************************************

import java.sql.*; // Needed for JDBC classes import javax.swing.*; // Needed for Swing classes import java.awt.*; // Needed for the BorderLayout class import java.awt.event.*; // Needed for event listener interface

/** This program demonstrates a solution to the PhoneBook Database programming challenge. */

public class PhoneBookDemo extends JFrame { PhoneBookManager pbManager;// A connection to the database JList nameList; // A list to hold the names JScrollPane scrollPane; // A scroll pane to hold the list JPanel listPanel; // A panel to hold the scroll pane JPanel buttonPanel; // A panel to hold the buttons JButton newButton; // A button to add a new entry JButton deleteButton; // A button to delete an entry JButton viewButton; // A button to view an entry JButton editButton; // A button to edit an entry JButton exitButton; // A button to exit the program

/** Constructor */ public PhoneBookDemo() { // Set the window title. setTitle("Phone Book Database"); // Specify an action for the close button. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Build the list panel. buildListPanel(); // Build the button panel. buildButtonPanel(); // Add the button panel. add(listPanel, BorderLayout.CENTER); add(buttonPanel, BorderLayout.SOUTH); // Pack and display. pack(); setVisible(true); } /** The buildListPanel method builds a panel to hold the list of names that will be displayed to the user. */

private void buildListPanel() { try { // Create a panel. listPanel = new JPanel(); // Add a titled border to the panel. listPanel.setBorder(BorderFactory. createTitledBorder("Select a Name")); // Create an PhoneBookManager object. pbManager = new PhoneBookManager(); // Create a JList object to hold the names. nameList = new JList(pbManager.getNames()); // Set the number of visible rows. nameList.setVisibleRowCount(5); // Put the JList object in a scroll pane. scrollPane = new JScrollPane(nameList); // Add the scroll pane to the panel. listPanel.add(scrollPane); } catch(SQLException ex) { // If something goes wrong with the database, // display a message to the user. JOptionPane.showMessageDialog(null, ex.toString()); } }

/** The buildButtonPanel method builds a panel to hold the buttons. */

private void buildButtonPanel() { // Create a panel. buttonPanel = new JPanel();

// Add a titled border to the panel. buttonPanel.setBorder(BorderFactory. createTitledBorder("Select an Action")); // Create the buttons. newButton = new JButton("New"); viewButton = new JButton("View"); deleteButton = new JButton("Delete"); editButton = new JButton("Edit"); exitButton = new JButton("Exit"); // Register the action listeners for the buttons. newButton.addActionListener(new NewButtonListener()); viewButton.addActionListener(new ViewButtonListener()); deleteButton.addActionListener(new DeleteButtonListener()); editButton.addActionListener(new EditButtonListener()); exitButton.addActionListener(new ExitButtonListener());

// Add the buttons to the panel. buttonPanel.add(newButton); buttonPanel.add(viewButton); buttonPanel.add(deleteButton); buttonPanel.add(editButton); buttonPanel.add(exitButton); }

/** The NewButtonListener class is an action listener for the New button. */

private class NewButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // Get a new name from the user. String newName = JOptionPane.showInputDialog(null, "Enter a new name."); // Determine that the name has a value. if(newName != null) { // Get a new phone number from the user. String newNumber = JOptionPane.showInputDialog(null, "Enter a new phone number."); // Determine that the phone number has a value. if(newNumber != null) { // Create a new PhoneBookManager object // and perform the INSERT INTO operation. pbManager = new PhoneBookManager(); pbManager.addEntry(newName, newNumber); // Notify the user that the operation was a success. JOptionPane.showMessageDialog(null, "Success! The item was added to the database."); // Create a new PhoneBookManager Object and // update the name list with the names in the // database. pbManager = new PhoneBookManager(); nameList.setListData(pbManager.getNames()); } } } catch(SQLException ex) { // If something goes wrong with the database, // display a message to the user. JOptionPane.showMessageDialog(null, ex.toString()); } } } /** The ViewButtonListener class is an action listener for the View button. */

private class ViewButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // Get the selected value from the name list. String selectedName = (String) nameList.getSelectedValue(); // Determine that the name has a value. if(selectedName != null) { // Create a new PhoneBookManager object // and get the phone number from the // database. pbManager = new PhoneBookManager(); String number = pbManager.getPhoneNumber(selectedName); // Display the information to the user. JOptionPane.showMessageDialog(null, selectedName + " " + number); } else // Display a message indicating that a // name must be selected from the list. JOptionPane.showMessageDialog(null, "Select a name from the list."); } catch(SQLException ex) { // If something goes wrong with the database, // display a message to the user. JOptionPane.showMessageDialog(null, ex.toString()); } } }

/** The DeleteButtonListener class is an action listener for the Delete button. */

private class DeleteButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // Get the selected value from the name list. String selectedName = (String) nameList.getSelectedValue(); // Determine that the name has a value. if(selectedName != null) { // Create a new PhoneBookManager object // and perform the DELETE FROM operation. pbManager = new PhoneBookManager(); pbManager.deleteEntry(selectedName); // Notify the user that the operation was a success. JOptionPane.showMessageDialog(null, "Success! The item was removed from " + " the database."); // Create a new PhoneBookManager Object and // update the name list with the names in the // database. pbManager = new PhoneBookManager(); nameList.setListData(pbManager.getNames()); } else // Display a message indicating that a // name must be selected from the list. JOptionPane.showMessageDialog(null, "Select a name from the list."); } catch(SQLException ex) { // If something goes wrong with the database, // display a message to the user. JOptionPane.showMessageDialog(null, ex.toString()); } } }

/** The EditButtonListener class is an action listener for the edit button. */

private class EditButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // Get the selected value from the name list. String selectedName = (String) nameList.getSelectedValue(); // Create a new PhoneBookManager Object and // connect to the database. pbManager = new PhoneBookManager(); // Determine that the name has a value. if(selectedName != null) { // Display the existing information in the database // and get a new phone number from the user. String newNumber = JOptionPane.showInputDialog(null, selectedName + " " + pbManager.getPhoneNumber(selectedName) + " Enter a new phone number."); // Determine that the new phone number has a value. if(newNumber != null) { // Perform the UPDATE operation. pbManager.setPhoneNumber(selectedName, newNumber); // Notify the user that the operation was a success. JOptionPane.showMessageDialog(null, "Success! The item was updated in the database."); } } else // Display a message indicating that a // name must be selected from the list. JOptionPane.showMessageDialog(null, "Select a name from the list."); } catch(SQLException ex) { // If something goes wrong with the database, // display a message to the user. JOptionPane.showMessageDialog(null, ex.toString()); } } }

/** The ExitButtonListener class is an action listener for the Exit button. */

private class ExitButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Exit the application. System.exit(0); } }

/** The main method */

public static void main(String[] args) { // Instantiate the class anonymously. new PhoneBookDemo(); } }

***********************************************************************************************************************************

import java.sql.*;

/** The PhoneBookManager class performs operations on the PhoneBook database for the PhoneBook Database programming challenge. */

public class PhoneBookManager { // Constant for database URL. public final String DB_URL = "jdbc:derby:PhoneBook";

// Field for the database connection private Connection conn;

/** Constructor */

public PhoneBookManager() throws SQLException { // Create a connection to the database. conn = DriverManager.getConnection(DB_URL); }

/** The getNames method returns an array of Strings containing all the names. */

public String[] getNames() throws SQLException { // Create a Statement object for the query. Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

// Execute the query. ResultSet resultSet = stmt.executeQuery( "SELECT Name FROM Entries");

// Get the number of rows resultSet.last(); // Move to last row int numRows = resultSet.getRow(); // Get row number resultSet.first(); // Move to first row

// Create an array for the names. String[] listData = new String[numRows];

// Populate the array with names. for (int index = 0; index < numRows; index++) { // Store the name in the array. listData[index] = resultSet.getString(1);

// Go to the next row in the result set. resultSet.next(); }

// Close the connection and statement objects. conn.close(); stmt.close();

// Return the listData array. return listData; }

/** The getPhoneNumber method returns a specific person's phone number. @param name The specified name. */

public String getPhoneNumber(String name) throws SQLException { String phoneNumber = ""; // Phone number

// Create a connection to the database. conn = DriverManager.getConnection(DB_URL);

// Create a Statement object for the query. Statement stmt = conn.createStatement();

// Execute the query. ResultSet resultSet = stmt.executeQuery( "SELECT PhoneNumber " + "FROM Entries " + "WHERE Name = '" + name + "'");

// If the result set has a row, go to it // and retrieve the phone number. if (resultSet.next()) phoneNumber = resultSet.getString(1);

// Close the Connection and Statement objects. conn.close(); stmt.close();

// Return the phone number. return phoneNumber; }

/** The setPhoneNumber method sets a specific person's phone number. @param name The specified person's name. @param number The new value for the phone number. */

public void setPhoneNumber(String name, String newNumber) throws SQLException { // Create a connection to the database. conn = DriverManager.getConnection(DB_URL);

// Create a Statement object for the query. Statement stmt = conn.createStatement();

// Execute the update. stmt.executeUpdate("UPDATE Entries " + "SET PhoneNumber = '" + newNumber + "' WHERE Name = '" + name + "'");

// Close the Connection and Statement objects. conn.close(); stmt.close(); } /** The addEntry method adds a new entry to the Entries table in the PhoneBook database. @param name The person's name. @param phoneNumber The person's phone number. */

public void addEntry(String name, String phoneNumber) throws SQLException { // Create a connection to the database. conn = DriverManager.getConnection(DB_URL);

// Create a Statement object for the query. Statement stmt = conn.createStatement();

// Execute the query. stmt.executeUpdate("INSERT INTO Entries VALUES ('" + name + "', '" + phoneNumber + "')");

// Close the connection and statement objects. conn.close(); stmt.close(); } /** The deleteEntry method removes an existing entry from the Entries table in the PhoneBook database. @param name The person's name. */

public void deleteEntry(String name)throws SQLException { // Create a connection to the database. conn = DriverManager.getConnection(DB_URL);

// Create a Statement object for the query. Statement stmt = conn.createStatement();

// Execute the query. stmt.executeUpdate("DELETE FROM Entries WHERE Name = '" + name + "'");

// Close the connection and statement objects. conn.close(); stmt.close(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!