jdbc,數據庫案例:客戶信息管理系統:業務層接口和實現,數據控制servlet

業務層接口和實現:html

public interface BussinessService {
 /**
  * 查詢全部客戶信息
  * @return
  */
 @Deprecated
 List<Customer> findAll();
 /**
  * 增長客戶信息
  * @param  c
  */
 void addCustomer(Customer c);
 /**
  * 根據主鍵刪除客戶信息
  * @param  customerId
  */
 void delCustomer(String customerId);
 /**
  * 分居主鍵查詢客戶信息
  * @param  customerId
  * @return  null
  */
 Customer findCustomerById(String customerId);
 /**
  * 更新客戶信息
  * @param  c
  * @throws  CustomerIdCannotBeEmpty
  */
 void updateCustomer(Customer c) throws CustomerIdCannotBeEmpty;
 /**
  * 輸入頁碼,
  * @param  pageNum
  * @return  返回pange對象
  */
 Page findPage(String num);
}app

public class BussinessServletImpl implements BussinessService {
 
 private CustomerDao dao=new CustomerDaoImpl();
 @Deprecated
 public List<Customer> findAll() {
  return dao.findAll();
 }dom

 public void addCustomer(Customer c) {
  c.setId(UUID.randomUUID().toString());
  dao.add(c);
 }jsp

 public void delCustomer(String customerId) {
  dao.delete(customerId);
 }spa

 public Customer findCustomerById(String customerId) {
  return dao.findById(customerId);
 }.net

 public void updateCustomer(Customer c) throws CustomerIdCannotBeEmpty {
  if(c.getId()==null){
   throw new CustomerIdCannotBeEmpty("參數有誤,請輸入正確的客戶信息");
  }
  dao.update(c);
 }orm

 public Page findPage(String num) {
  int pageNum=1;//默認值爲1
  if(num!=null){
   pageNum=Integer.parseInt(num);
  }
  int totalRecords=dao.getTotalRecordsNum();
  Page page=new Page(pageNum, totalRecords);
  List<Customer> records=dao.findPageCustomers(page.getStartIndex(), page.getPageSize());
  page.setRecords(records);
  return page;
 }htm

}
對象

數據控制servlet:接口

public class Controller extends HttpServlet {
 private BussinessService bs=new BussinessServletImpl();
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  response.setContentType("text/html;charset=UTF-8");
  String op=request.getParameter("op");
  if("showAllCustomers".equals(op)){
   showAllCustomers(request,response);
  }else if("addCustomer".equals(op)){
   addCustomer(request,response);
  }else if("editCustomerUI".equals(op)){
   editCustomerUI(request,response);
  }else if("editCustomer".equals(op)){
   editCustomer(request,response);
  }else if("delOneCustomer".equals(op)){
   delOneCustomer(request,response);
  }else if("delMulti".equals(op)){
   delMulti(request,response);
  }
 }

 

 private void delMulti(HttpServletRequest request,
   HttpServletResponse response) throws IOException  {
  String ids[]=request.getParameterValues("ids");
  if(ids!=null&&ids.length>0){
   for(int i=0;i<ids.length;i++){
    bs.delCustomer(ids[i]);
   }
  }
  response.sendRedirect(request.getContextPath());
 }

 

 private void delOneCustomer(HttpServletRequest request,
   HttpServletResponse response) throws IOException {
  String customerId=request.getParameter("customerId");
  bs.delCustomer(customerId);
  response.sendRedirect(request.getContextPath());
 }
 
 
 private void editCustomer(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException{
    //將信息封裝到formBean
    CustomerFormBean formBean=WebUtil.fillBean(request, CustomerFormBean.class);
    //信息不正確,數據回顯
    if(!formBean.validate()){
     request.setAttribute("formBean", formBean);
     request.getRequestDispatcher("/addCustomer.jsp").forward(request, response);
    }
    //填充模型,注意類型轉化
    ConvertUtils.register(new DateLocaleConverter(),Date.class);
    Customer c=new Customer();
    try {
     BeanUtils.copyProperties(c, formBean);
    } catch (Exception e) {
     throw new RuntimeException("填充模型時出現異常");
    }
    //單獨處理愛好選項
    String preference[]=request.getParameterValues("preference");
    if(preference!=null&&preference.length>0){
     StringBuffer sb=new StringBuffer();
     for(int i=0;i<preference.length;i++){
      if(i>0){
       sb.append(",");
      }
      sb.append(preference[i]);
     }
     c.setPreference(sb.toString());
    }
    //保存數據
    try {
     bs.updateCustomer(c);
    } catch (CustomerIdCannotBeEmpty e) {
     e.printStackTrace();
    }
    //用重對象將頁面跳轉到默認主頁,防止重複提交
    response.sendRedirect(request.getContextPath());
 }

 

 private void editCustomerUI(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  String customerId=request.getParameter("customerId");
  Customer c=bs.findCustomerById(customerId);
  request.setAttribute("c", c);
  request.getRequestDispatcher("/editCustomer.jsp").forward(request, response);
 }
 private void addCustomer(HttpServletRequest request,
   HttpServletResponse response) throws IOException, ServletException  {
  //將信息封裝到formBean
  CustomerFormBean formBean=WebUtil.fillBean(request, CustomerFormBean.class);
  //信息不正確,數據回顯
  if(!formBean.validate()){
   request.setAttribute("formBean", formBean);
   request.getRequestDispatcher("/addCustomer.jsp").forward(request, response);
  }
  //填充模型,注意類型轉化
  ConvertUtils.register(new DateLocaleConverter(),Date.class);
  Customer c=new Customer();
  try {
   BeanUtils.copyProperties(c, formBean);
  } catch (Exception e) {
   throw new RuntimeException("填充模型時出現異常");
  }
  //單獨處理愛好選項
  String preference[]=request.getParameterValues("preferences");
  if(preference!=null&&preference.length>0){
   StringBuffer sb=new StringBuffer();
   for(int i=0;i<preference.length;i++){
    if(i>0){
     sb.append(",");
    }
    sb.append(preference[i]);
   }
   c.setPreference(sb.toString());
  }
  //保存數據
  bs.addCustomer(c);
  //用重對象將頁面跳轉到默認主頁,防止重複提交
  response.sendRedirect(request.getContextPath());
 }

 private void showAllCustomers(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException{
  List<Customer> cs=bs.findAll();
  request.setAttribute("cs", cs);
  String num=request.getParameter("num");
  Page page=bs.findPage(num);
  page.setServletUrl("/servlet/Controller?op=showAllCustomers");
  request.setAttribute("page", page);
  request.getRequestDispatcher("/listCustomers.jsp").forward(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }

}

相關文章
相關標籤/搜索