Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Operation This application begins by displaying a table of customer data. If the

ID: 3577463 • Letter: O

Question

Operation

This application begins by displaying a table of customer data.

If the user clicks the Add button, the application allows the user to add customer data to the table (and the underlying database).

If the user selects a customer row and clicks the Edit button, the application allows the user to update the data for the selected customer row in the table (and the database).

If the user selects a customer row and clicks the Delete button, the application deletes the selected customer row from the table (and the database).

Specifications

Create a table in the mma database described in chapter 19 to store the necessary data. To do that, you can use the SQL script stored in the create_customer_table.sql file that’s supplied. If this script isn’t supplied, you can create your own SQL script.

Create a class named Customer that stores data for the user’s id, email address, first name, and last name.

Create a class named CustomerDB that contains the methods necessary to get an array list of Customer objects, to get a Customer object for the customer with the specified id, and to add, update, or delete the specified customer.

Create a CustomerManagerFrame class like the one shown above. This frame should display a table of customer data as well as the Add, Edit, and Delete buttons. This class should use the Customer and CustomerDB classes to work with the customer data.

Create a CustomerForm class that allows the user to add or edit customer data.

Create a Java Customer Management program with NetBeans that satisfies the specifications above.

Show transcribed image text

Customer Manager Email frankiones nes@yahoo.com johnsmith@hotmail.com seagreen@levi.com wendyk@warners.com First Name Last Name Frank Jones John Smith Green Cynthia Wendy Kowolski Add Eidt Delete

Explanation / Answer

Customer table creation in SQL

SQL>CREATE TABLE CUSTOMER(

ID        INT     NOT NULL,

F_NAME        VARCHAR(30)         NOT NULL,

L_NAME        VARCHAR(30)         NOT NULL,

EMAIL           VARCHAR(50) ,

PRIMERY KEY(ID));

//customer class
public class Customer {
   protected int customerNo;
   protected String fname,lname,email;
     
   public Customer(int customerNo, String fname, String lname, String email) {
       super();
       this.customerNo = customerNo;
       this.fname = fname;
       this.lname = lname;
       this.email = email;
   }
   public Customer() {
       super();
       // TODO Auto-generated constructor stub
   }
   public int getCustomerNo() {
       return customerNo;
   }
   public void setCustomerNo(int customerNo) {
       this.customerNo = customerNo;
   }
   public String getEmail() {
       return email;
   }
   public void setEmail(String email) {
       this.email = email;
   }
   public String getFname() {
       return fname;
   }
   public void setFname(String fname) {
       this.fname = fname;
   }
   public String getLname() {
       return lname;
   }
   public void setLname(String lname) {
       this.lname = lname;
   }
}


//customerDB class
import java.sql.*;

public class CustomerDB {
   public static PreparedStatement ps=null;
   public static String url = "jdbc:mysql://localhost:3306/test?requireSSL=false&useUnicode=true&characterEncoding=utf8";
   public static Connection con=null;
  
public static void getConnection(){
               Class.forName("com.mysql.jdbc.Driver");
               con= DriverManager.getConnection(url, "root", "");
  
   }

public static void addCustomer(Customer c) throws SQLException{
               ps = con.prepareStatement("Insert into customer(id,fname,lname,email) values(?,?,?,?);");
               ps.setInt(1,(Integer) c.getCustomerNo());
               ps.setString(2, (String) c.getFname());
               ps.setString(3, (String) c.getLname());
               ps.setString(4, (String) c.getEmail());
               ps.executeUpdate();
               ps.close();
}
public static void editCustomer(Customer c)throws SQLException{
           ps = con.prepareStatement("update customer SET fname = ?,lname = ?,email = ? WHERE id = ;"+c.getCustomerNo());
           ps.setString(1, (String) c.getFname());
           ps.setString(2, (String) c.getLname());
           ps.setString(3, (String) c.getEmail());
           ps.executeUpdate();
           ps.close();
}

  
public static Object getCustomers(int custno)throws SQLException{
       ps = con.prepareStatement("select * from customer where id=;"+custno);
       ResultSet customers= ps.executeQuery();
       ps.close();
       return customers.getObject(0);
}

public static void delCustomer(int custNo)throws SQLException{
       ps = con.prepareStatement("delete frome customer WHERE id = "+custNo);
       ps.executeUpdate();
       ps.close();
}
}

   //customer manager Frame.
   import java.awt.FlowLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import javax.swing.JButton;
   import javax.swing.JFrame;
import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

  
public class CustomerManagerFrame extends JFrame implements ActionListener{

      protected JTable jcustTable;
      protected JButton jaddButton,jeditButton,jdelButton;
      protected JPanel jtabPanel,jbutPanel,jaddPanel;
      protected JLabel jemailLabel,jfnameLabel,jlnameLabel;
      protected JTextField jemailTextField,jfnameTextField,jlnameTextField;
      protected JFrame jaddFrame;
      protected CustomerTableModel ctm;
     
      //initializing the frame
      public void start() {

      jaddButton=new JButton();
       jaddButton.setText("ADD");
       jaddButton.addActionListener(this);
          
       jeditButton=new JButton();
       jeditButton.setText("EDIT");
       jeditButton.addActionListener(this);
          
       jdelButton=new JButton();
       jdelButton.setText("DELETE");
       jdelButton.addActionListener(this);
             
       jbutPanel=new JPanel();
       jbutPanel.add(jaddButton,0);
       jbutPanel.add(jeditButton,1);
       jbutPanel.add(jdelButton,2);
             
       jtabPanel= new JPanel();
       ctm=new CustomerTableModel();
       jcustTable= new JTable(ctm.customers,ctm.columnNames);
       jcustTable.setModel(ctm);
       JScrollPane scrollPane = new JScrollPane(jcustTable);
      
       TableColumn column;
       //setting the email column to bigger.
       for (int i = 0; i < 3; i++) {
           column = jcustTable.getColumnModel().getColumn(i);
           if (i == 0) {
               column.setPreferredWidth(100); //third column is bigger
           } else {
               column.setPreferredWidth(50);
           }
       }
                  
       jtabPanel.add(scrollPane);
             
       jemailLabel=new JLabel("EMail");
       jemailTextField=new JTextField();
          
       jfnameLabel=new JLabel("First Name");
       jfnameTextField=new JTextField();
          
       jlnameLabel=new JLabel("Last Name");
       jlnameTextField=new JTextField();
              
       jaddPanel=new JPanel();
       jaddPanel.add(jemailLabel);
       jaddPanel.add(jemailTextField);
       jaddPanel.add(jfnameLabel);
       jaddPanel.add(jfnameTextField);
       jaddPanel.add(jlnameLabel);
       jaddPanel.add(jlnameTextField);
      
      
       //creating for popup frame for adding new customer.
       jaddFrame=new JFrame();
       jaddFrame.add(jaddPanel);
       jaddFrame.setLayout(new FlowLayout());
       jaddFrame.setTitle("ADD CUSTOMER");
       jaddFrame.setSize(200,200);
       jaddFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
       jaddFrame.setVisible(false);
      
       add(jtabPanel,0);
       add(jbutPanel,1);
       jcustTable.setSelectionMode(1);  
             
       setLayout(new FlowLayout());
       setTitle("Customer Manager");
       setSize(400,400);
       setVisible(true);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
public void actionPerformed(ActionEvent e){
  
  
   //adding customers to the frame
   if(e.getActionCommand()=="ADD"){
       System.out.print(" adding");
               jaddFrame.setVisible(true);
               int i=ctm.getRowCount();
               if(jfnameTextField.toString().trim()!=""&&jlnameTextField.toString().trim()!="" &&jemailTextField.toString().trim()!=""){
               Object c[][];
              
               //getting the customer info from popup frame
               c[i][0]=i+1;
               c[i][1]=jfnameTextField.toString().trim();
               c[i][2]=jlnameTextField.toString().trim();
               c[i][3]=jemailTextField.toString().trim();
              
               //adding the customer info into table.
               ((DefaultTableModel)jcustTable.getModel()).insertRow(i+1,c );
               ((DefaultTableModel)jcustTable.getModel()).fireTableDataChanged();;
              
               Customer cust=new Customer(i+1,jfnameTextField.toString().trim(),jlnameTextField.toString().trim(),jemailTextField.toString().trim());
               //adding the customer info into customerDB
               CustomerDB.addCustomer(cust);
          
   }
//editing the customer information and saving into customerDB
   else if(e.getActionCommand()=="EDIT"){
       System.out.print(" editing");
      
       //getting the selected row
       i=jcustTable.getSelectedRow();
       jcustTable.setEditingRow(i);
      
       //getting the updated customer info
       ctm.customers[i][0]=jcustTable.getModel().getValueAt(i,0);
       ctm.customers[i][1]=jcustTable.getModel().getValueAt(i,1);  
       ctm.customers[i][2]=jcustTable.getModel().getValueAt(i,2);  
       ctm.customers[i][3]=jcustTable.getModel().getValueAt(i,3);
       ((DefaultTableModel)jcustTable.getModel()).fireTableDataChanged();
       //setting the updated customer info into customer table.
       ((DefaultTableModel)jcustTable.getModel()).setDataVector(ctm.customers,ctm.columnNames );
       Customer cust=new Customer((Integer)ctm.customers[i][0],(String)ctm.customers[i][1],(String)ctm.customers[i][2],(String)ctm.customers[i][3]);
      
       //updating the selected customer info
       CustomerDB.editCustomer(cust);
      
   }
  
   //removing the selected customer information from both table and customerDB
  
   else if(e.getActionCommand()=="DELETE"){
       System.out.print(" deleting");
      
       //getting the selected customer info
       i=jcustTable.getSelectedRow();
      
       //getting the selected customer number
       Object cno=jcustTable.getModel().getValueAt(i,0);
       //removing the selected customer from customer table.
       ((DefaultTableModel)jcustTable.getModel()).removeRow(i);
       ((DefaultTableModel)jcustTable.getModel()).fireTableRowsDeleted(i,i);
      
       //removing the selected customer from customerDB.
       CustomerDB.delCustomer((Integer)cno);
   }
              
  

}
   public static void main(String args[]) {
       new CustomerManagerFrame().start();
       }
   }


//Customer Table Model
import javax.swing.table.DefaultTableModel;


public class CustomerTableModel extends DefaultTableModel{

  
   public String[] columnNames = {"Customer No","First Name",
"Last Name",
"E-Mail"};
   protected Object[][] customers={
           {1,"frank", "jones", "frankjones@yahoo.com"},
           {2,"john","smith","johnsmith@hotmail.com"},
           {3,"Cynthia","green","seagreen@levi.com"},
           {4,"frank","jones","frankjones@yahoo.com"},
   };
      
   public Object getCustomer(int row,int column){
       return customers[row][column];
   }
  
public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return customers.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return customers[row][col];
}

public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 1) {
return false;
} else {
return true;
}
}

/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Customer c, int row, int col) {
customers[row][col] = c;
fireTableCellUpdated(row, col);
}
}


  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote