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: 3577101 • 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.

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

TableNames.java

public enum TableNames {

      CUSTOMERDETAILS;

}

AddCustomer.java

import java.io.IOException;

import java.sql.SQLException;

import java.util.ArrayList;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class AddCustomer

*/

@WebServlet(name="AddCustomer",urlPatterns="/AddCustomer")

public class AddCustomer extends HttpServlet {

      private static final long serialVersionUID = 1L;

      private CreateDBConnection connection;

      private RequestDispatcher rd;

      /**

      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

      */

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            response.setContentType("text/HTML");

            FormData fd = new FormData();

            ArrayList<String> pnames = new ArrayList<String>();

            pnames = fd.getParameterNames(request);

            ArrayList<String> values = new ArrayList<String>();

            values = fd.getParameterValues(request, pnames);

            boolean flag;

            try {

                  connection=new CreateDBConnection();

                  //String tableName=values.get(2);

                  values.remove(0);

                  flag=connection.AddCustomer(TableNames.CUSTMERDETAILS.toString(),values);

                  connection.closeConnection();

                  if (flag) {

                        rd = request.getRequestDispatcher("afterAddCustomer.html");

                        rd.include(request, response);

                  } else {

                        rd = request.getRequestDispatcher("studentalreadyadded.html");

                        rd.include(request, response);

                  }

            } catch (SQLException e) {

                  // TODO Auto-generated catch block

                  //System.out.println("AddCustomer");

                  e.printStackTrace();

            }

      }

}

RemoveCustomer.java

import java.io.IOException;

import java.sql.SQLException;

import java.util.ArrayList;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class RemoveCustomer

*/

@WebServlet(name="remove customer",urlPatterns="/removecustomer")

public class RemoveCustomer extends HttpServlet {

      private static final long serialVersionUID = 1L;

      private CreateDBConnection connection;

      private RequestDispatcher rd;

      /**

      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

      */

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            response.setContentType("text/HTML");

            FormData fd = new FormData();

            ArrayList<String> pnames = new ArrayList<String>();

            pnames = fd.getParameterNames(request);

            ArrayList<String> values = new ArrayList<String>();

            values = fd.getParameterValues(request, pnames);

            boolean flag;

            try {

                  connection=new CreateDBConnection();

                  String tableName=TableNames.REMOVECUSTOMERDETAILS.toString();

                  //values.remove(0);

                  flag=connection.removeCustomer(tableName, values);

                  //int len=rs.getFetchSize();

                  if (flag) {

                              rd = request.getRequestDispatcher("afterremoveCustomer.html");

                              rd.include(request, response);

                        }else {

                              rd = request.getRequestDispatcher("wrongCustomerremoved.html");

                              rd.include(request, response);

                        }

                 

            } catch (SQLException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

            }

      }

}

CustomerDB.java

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

public class CustomerDB {

     

      private Connection con;

      private Statement stmt;

      private ResultSet rs=null;

      public CreateDBConnection() throws SQLException {

            try {

                  //System.out.println("ok....");

                  Class.forName("oracle.jdbc.driver.OracleDriver");

                  //System.out.println("Class loaded...");

                  con=DriverManager.getConnection(

                              "jdbc:oracle:thin:@localhost:1521:xe","system","sivadurga");

                  // System.out.println("Driver loaded...");

                  System.out.println("Connection created....");

                        stmt=con.createStatement();

                        System.out.println("Stmt created...");

                             

            } catch (ClassNotFoundException e) {

                  System.out.println("Connection not created...");

                  //e.printStackTrace();

            }

           

      }

      public ResultSet getResult(ArrayList<String> pnames) {

            String query="select";

            int len=pnames.size();

            int i;

            for(i=0;i<len-1;i++)

            {

                  query=query+pnames.get(i)+",";

            }

            query=query+pnames.get(i)+"from"+TableNames.CUSTOMERDETAILS.toString();

            try {

                  rs=stmt.executeQuery(query);

            } catch (SQLException e) {

                  //e.printStackTrace();

                  System.out.println("Exception");

                        return rs;

            }

           

            return rs;

      }

      public ResultSet getResult(String tableName,String columnName,String value) {

            String query="select * from "+tableName+" where "+columnName+" = '"+value+"'";

            System.out.println(query);

            try {

                  rs=stmt.executeQuery(query);

                  return rs;

                  //int n=rs.getFetchSize();

            } catch (SQLException e) {

                  //e.printStackTrace();

                  return rs;

            }

           

      }

      public ResultSet getResult(String tableName) {

            String query="select * from "+tableName;

            System.out.println(query);

            try {

                  rs=stmt.executeQuery(query);

                  return rs;

                  //int n=rs.getFetchSize();

            } catch (SQLException e) {

                  //e.printStackTrace();

                  return rs;

            }

      }

      public ResultSet getResult(String tableName,ArrayList<String> columnsList,String conditionColumnName,String value) {

            String query="select ";

            int len=columnsList.size();

            int i;

            for(i=0;i<len-1;i++)

            {

                  query=query+columnsList.get(i)+",";

            }

            query=query+columnsList.get(i)+" from "+tableName+" where "+conditionColumnName+" = "+"'"+value+"'";

            System.out.println(query);

            try {

                  rs=stmt.executeQuery(query);

                  return rs;

            } catch (SQLException e) {

                  //e.printStackTrace();

                  return rs;

            }

           

           

      }

      public boolean insertRow(String tableName,ArrayList<String> values) throws SQLException {

            String query="insert into "+tableName+" values(";

            int len=values.size();

            int i;

            for(i=0;i<len-1;i++)

            {

                  query=query+"'"+values.get(i)+"',";

            }

            query=query+"'"+values.get(i)+"')";

            System.out.println(query);

            try {

                  int r=stmt.executeUpdate(query);

                  //System.out.println(r);

                  if(r==1)

                  {

                        return true;

                  }

            } catch (SQLException e) {

                  //e.printStackTrace();

                  int rValue=e.getErrorCode();

                  //System.out.println(rValue);

                  if (rValue==942)

                  {

                        /*ArrayList<String> dataTypes=new ArrayList<String>();

                        for(int j=0;j<len;j++)

                        {

                              dataTypes.add("varchar2(20)");

                        }*/

                        boolean b;

                       

      private boolean createCustomerTable() {

            String query="create table "+TableNames.CUSTOMERDETAILS.toString()+"(FIRSTNAME varchar2(20) not null, LASTNAME varchar2(10) not null, ,EMAIL varchar2(32) not null);

int r;

            System.out.println(query);

            try {

                  r = stmt.executeUpdate(query);

                  //System.out.println(r);

                  if(r==0)

                  {

                        //System.out.println("table created...");

                        return true;

                  }

                 

            } catch (SQLException e) {

                  e.printStackTrace();

            }

            return false;

      }

     

      public boolean update(String tableName,ArrayList<String> cList,ArrayList<String> values,String status) {

            String query="update "+tableName+" set STATUS = '"+status+"' where ";

            int len=cList.size();

            int i;

            for(i=0;i<len-1;i++)

            {

                  query=query+cList.get(i)+" = '"+values.get(i)+"' and ";

            }

            query=query+cList.get(i)+" = '"+values.get(i)+"'";

            try {

                  System.out.println(query);

                  stmt.execute(query);

            } catch (SQLException e) {

                  e.printStackTrace();

                  return false;

            }

            return true;

           

      }

      public boolean deleteRow(String tableName,String rno) {

           

            return false;

           

           

      }

     

      public boolean removeCustomer(String tableName,ArrayList<String> values) {

            String s="select DOB from "+TableNames.REMOVECUSTOMERDETAILS.toString()+" where ROLLN = '1253'";

            try {

                  stmt.execute(s);

            } catch (SQLException e1) {

                  int r=e1.getErrorCode();

                  if(r==942)

                        createRemoveCustomerDetailsTable();

            }

           

            String q="select * from "+TableNames.CUSTOMERDETAILS.toString()+" where ROLLNO = '"+values.get(1)+"'";

            System.out.println(q);

            ResultSet rs=null,hd=null;

            try {

                  rs=stmt.executeQuery(q);

                  ResultSetMetaData rm=rs.getMetaData();

                  int cc=rm.getColumnCount();

                  String query="insert into "+TableNames.REMOVECUSTOMERDETAILS.toString()+" values(";

                  while(rs.next())

                  {

                        //System.out.println("sd");

                        for(int i=1;i<=cc;i++)

                        {

                              query=query+"'"+rs.getString(i)+"',";

                        }

                  }

                  query=query+"'"+values.get(2)+"',";

                  //System.out.println(query);

                  q="select ROOMNO,DOJ from "+TableNames.CUSTOMERDETAILS.toString()+" where ROLLNO = '"+values.get(1)+"'";

                  System.out.println(q);

                  hd=stmt.executeQuery(q);

                  boolean b = false;

                  System.out.println(rs);

                  System.out.println(hd);

                  //ResultSet hd2=hd;

                 

                  while(hd.next()){

                        int rno=hd.getInt("ROOMNO");

                        System.out.println(rno);

                        if(rno==Integer.parseInt(values.get(3)))

                        {

                              query=query+"'"+hd.getString("DOJ")+"',";

                              b=true;

                        }

                  }

                  if(b){

                        query=query+"'"+values.get(4)+"')";

                        System.out.println(query);

                        int r=stmt.executeUpdate(query);

                        if(r==1){

                              String deleteQuery="delete from "+TableNames.CUSTOMERDETAILS.toString()+" where ROLLNO = '"+values.get(1)+"'";

                              deleteRow(deleteQuery);

                              deleteQuery="delete from "+TableNames.CUSTOMERDETAILS.toString()+" where ROLLNO = '"+values.get(1)+"'";

                              deleteRow(deleteQuery);

                              return true;

                        }

                             

                             

                  }

            } catch (SQLException e) {

                  e.printStackTrace();

            }

            return false;

           

      }

      private void deleteRow(String deleteQuery) {

            try {

                  stmt.execute(deleteQuery);

            } catch (SQLException e) {

                  e.printStackTrace();

            }

      }

      private boolean createRemoveCustomerDetailsTable() {

            String query="create table "+TableNames.REMOVECUSTOMERDETAILS.toString()+"(FIRSTNAME varchar2(20) not null,LASTNAME varchar2(10) not null ,EMAIL varchar2(32) not null)";

            try {

                  int r=stmt.executeUpdate(query);

                  if(r==0)

                  {

                        return true;

                  }

            } catch (SQLException e) {

                  e.printStackTrace();

            }

            return false;

           

      }

     

      Editdetails.java

import java.io.IOException;

import java.util.ArrayList;

import java.util.Enumeration;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class editdetails

*/

@WebServlet(name="editdetails",urlPatterns="/editdetails")

public class editdetails extends HttpServlet {

      private static final long serialVersionUID = 1L;

      /**

      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

      */

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            response.setContentType("text/HTML");

            //Map map=request.getParameterMap();

            Enumeration<String> e=request.getParameterNames();

            ArrayList<String> pnames=new ArrayList<String>();

            while (e.hasMoreElements()) {

                  pnames.edit(e.nextElement());

            }

            ArrayList<String> values=new ArrayList<String>();

            int len=pnames.size();

            int i=0;

            while (i<len) {

                  values.edit(request.getParameter(pnames.get(i)));

                  i++;

            }

      }

}

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