Java 如何实现后端分页

1,457 阅读49分钟
原文链接: blog.csdn.net

工具:myeclipse ,数据库:Oracle ,jar包:classes12.jar

实现的功能:对客户的增删改查,展示客户列表,一页显示十条客户数据,实现分条件查询(根据ID,名称等)

这个小Demo用到了:

1,使用jsp+servlet,工厂模式,代理类

2,后端分页技术

3,ajax前后台交互

代码如下:

  1. /** 
  2.  *客户接口类 
  3.  */  
  4. public interface CustomerDAO {  
  5.     /** 
  6.      * @param pojo 客户类 
  7.      * @return 新增客户 
  8.      */  
  9.     public boolean doIns(CustomerPOJO pojo);  
  10.     /** 
  11.      * @param customerId  客户id 
  12.      * @return 删除客户 
  13.      */  
  14.     public boolean doDel(BigDecimal customerId);  
  15.     /** 
  16.      * @param pojo  客户类 
  17.      * @return 更新客户信息 
  18.      */  
  19.     public boolean doUpd(CustomerPOJO pojo);  
  20.     /** 
  21.      * @param customerId 
  22.      * @return 根据客户id,名称查询客户所有信息 
  23.      */  
  24.     public CustomerPOJO findById(BigDecimal cid);  
  25.     /** 
  26.      * @param customerName 
  27.      * @return 根据客户名查询全部信息 
  28.      */  
  29.     public CustomerPOJO findByName(String cname);  
  30.     /** 
  31.      * 列出所有客户 
  32.      */  
  33.     public List<CustomerPOJO> findAll(int pageSize, int pageCurrent);  
  34.       
  35.     /** 
  36.      *查询客户数量 
  37.      */  
  38.     public int findAllCount();  
  39. }  
/**
 *客户接口类
 */
public interface CustomerDAO {
	/**
	 * @param pojo 客户类
	 * @return 新增客户
	 */
	public boolean doIns(CustomerPOJO pojo);
	/**
	 * @param customerId  客户id
	 * @return 删除客户
	 */
	public boolean doDel(BigDecimal customerId);
	/**
	 * @param pojo  客户类
	 * @return 更新客户信息
	 */
	public boolean doUpd(CustomerPOJO pojo);
	/**
	 * @param customerId
	 * @return 根据客户id,名称查询客户所有信息
	 */
	public CustomerPOJO findById(BigDecimal cid);
	/**
	 * @param customerName
	 * @return 根据客户名查询全部信息
	 */
	public CustomerPOJO findByName(String cname);
	/**
	 * 列出所有客户
	 */
	public List<CustomerPOJO> findAll(int pageSize, int pageCurrent);
	
	/**
	 *查询客户数量
	 */
	public int findAllCount();
}
接口实现类:

  1. public class CustomerDAOImpl implements CustomerDAO {  
  2.    Connection conn;  
  3.    public CustomerDAOImpl(Connection conn){  
  4.       this.conn = conn;  
  5.    }  
  6.    public boolean doIns(CustomerPOJO pojo) {  
  7.       boolean flag=false;  
  8.       PreparedStatement pstate = null;  
  9.       try {  
  10.          this.conn.setAutoCommit(false);  
  11.          String sql="insert into customer(customer_id,customer_name,customer_sex,customer_tel,customer_adress,customer_pro_id)values(DH1.nextval,?,?,?,?,?)";  
  12.          pstate = this.conn.prepareStatement(sql);  
  13.          pstate.setString(1,pojo.getCustomerName());  
  14.          pstate.setInt(2,pojo.getCustomerSex());  
  15.          pstate.setString(3,pojo.getCustomerTel());  
  16.          pstate.setString(4,pojo.getCustomerAdress());  
  17.          pstate.setBigDecimal(5,pojo.getCustomerProId());  
  18.          pstate.execute();  
  19.          this.conn.commit();  
  20.          flag = true;  
  21.       } catch (SQLException e) {  
  22.          e.printStackTrace();  
  23.          try {  
  24.             this.conn.rollback();  
  25.          } catch (SQLException e1) {  
  26.             e1.printStackTrace();  
  27.          }  
  28.       }finally{  
  29.          try {  
  30.             pstate.close();  
  31.          } catch (SQLException e) {  
  32.             e.printStackTrace();  
  33.          }  
  34.       }  
  35.       return flag;  
  36.    }  
  37.    
  38.    public boolean doDel(BigDecimal customerId) {//伪删除,使信息不可见,方便误删找回  
  39.       boolean flag = false;  
  40.       PreparedStatement pstate = null;  
  41.       try {  
  42.          this.conn.setAutoCommit(false);  
  43.          String sql = "update customer set is_delete=0 wherecustomer_id = ?";  
  44.          pstate = this.conn.prepareStatement(sql);  
  45.          pstate.setBigDecimal(1, customerId);  
  46.          pstate.execute();  
  47.          this.conn.commit();  
  48.          flag = true;  
  49.       } catch (SQLException e) {  
  50.          e.printStackTrace();  
  51.          try {  
  52.             this.conn.rollback();  
  53.          } catch (Exception e2) {  
  54.             e2.printStackTrace();  
  55.          }  
  56.       } finally{  
  57.          try {  
  58.             pstate.close();  
  59.          } catch (Exception e2) {  
  60.             e2.printStackTrace();  
  61.          }  
  62.       }  
  63.       return flag;  
  64.    }  
  65.    
  66.    public boolean doUpd(CustomerPOJO pojo) {  
  67.       boolean flag = false;  
  68.       PreparedStatement pstate = null;  
  69.       try {  
  70.          this.conn.setAutoCommit(false);  
  71.          String sql = "update customer setcustomer_name=?,password=?,customer_sex=?,customer_tel=?,customer_adress=?,customer_pro_id=?,is_delete=?,role_mark=?where customer_id=?";  
  72.          pstate = this.conn.prepareStatement(sql);  
  73.          pstate.setBigDecimal(1,pojo.getCustomerId());  
  74.          pstate.setString(2,pojo.getCustomerName());  
  75.          pstate.setString(3,pojo.getPassword());  
  76.          pstate.setInt(4,pojo.getCustomerSex());  
  77.          pstate.setString(5,pojo.getCustomerTel());  
  78.          pstate.setString(6, pojo.getCustomerAdress());  
  79.          pstate.setBigDecimal(7,pojo.getCustomerProId());  
  80.          pstate.setInt(8, pojo.getIsDelete());  
  81.          pstate.setInt(9, pojo.getRoleMark());  
  82.          pstate.execute();  
  83.          this.conn.commit();  
  84.          flag = true;  
  85.       } catch (Exception e) {  
  86.          e.printStackTrace();  
  87.          try {  
  88.             this.conn.rollback();  
  89.          } catch (Exception e2) {  
  90.             e2.printStackTrace();  
  91.          }  
  92.       } finally{  
  93.          try {  
  94.             pstate.close();  
  95.          } catch (Exception e2) {  
  96.             e2.printStackTrace();  
  97.          }  
  98.       }  
  99.       return flag;  
  100.    }  
  101.    
  102.    public CustomerPOJO findById(BigDecimal customerId) {  
  103.       CustomerPOJO pojo = null;  
  104.       PreparedStatement pstate = null;  
  105.       ResultSet res = null;  
  106.       String sql = "select customer_name, password,customer_sex, customer_tel, customer_adress, customer_pro_id, is_delete, role_markfrom customer where customer_id = ? and is_delete=1";  
  107.       try {  
  108.          pstate = this.conn.prepareStatement(sql);  
  109.          pstate.setBigDecimal(1, customerId);  
  110.          res = pstate.executeQuery();  
  111.          while(res.next()){  
  112.             pojo=new CustomerPOJO(customerId,res.getString(1),res.getString( 2),res.getInt(3),res.getString(4),res.getString(5),res.getBigDecimal( 6),res.getInt(7),res.getInt(8));  
  113.              
  114.          }  
  115.       } catch (SQLException e) {  
  116.          e.printStackTrace();  
  117.       }finally{  
  118.          try {  
  119.             res.close();  
  120.             pstate.close();  
  121.          } catch (Exception e2) {  
  122.             e2.printStackTrace();  
  123.          }  
  124.       }  
  125.       return pojo;  
  126.    }  
  127.    
  128.    public CustomerPOJO findByName(String customerName) {  
  129.       CustomerPOJO pojo = null;  
  130.       PreparedStatement pstate = null;  
  131.       ResultSet res = null;  
  132.       String sql = "select customer_id, password,customer_sex, customer_tel, customer_adress, customer_pro_id, is_delete,role_mark from customer where customer_name = ? and is_delete=1";  
  133.       try {  
  134.          pstate = this.conn.prepareStatement(sql);  
  135.          pstate.setString(1, customerName);  
  136.          res = pstate.executeQuery();  
  137.          while(res.next()){  
  138.             pojo=newCustomerPOJO(res.getBigDecimal(1),customerName,res.getString(2),res.getInt( 3),res.getString(4),res.getString(5),res.getBigDecimal(6),res.getInt( 7),res.getInt(8));  
  139.              
  140.          }  
  141.       } catch (SQLException e) {  
  142.          e.printStackTrace();  
  143.       }finally{  
  144.          try {  
  145.             res.close();  
  146.             pstate.close();  
  147.          } catch (Exception e2) {  
  148.             e2.printStackTrace();  
  149.          }  
  150.       }  
  151.       return pojo;  
  152.    }  
  153.    public List<CustomerPOJO> findAll(int pageSize, int pageCurrent) {  
  154.       List<CustomerPOJO> list = new ArrayList<CustomerPOJO>();  
  155.       PreparedStatement pstate = null;  
  156.       ResultSet res = null;  
  157.       String sql = "select customer_id, customer_name,password, customer_sex, customer_tel, customer_adress, customer_pro_id,is_delete, role_mark  from " +  
  158.            "(select customer_id, customer_name,password, customer_sex, customer_tel, customer_adress, customer_pro_id,is_delete, role_mark, rownum as rn from customer)  where rn > ? andrn<=? and is_delete = 1" ;  
  159.       try {  
  160.          pstate = this.conn.prepareStatement(sql);  
  161.          pstate.setInt(1,(pageCurrent-1)*pageSize);  
  162.          pstate.setInt(2, pageCurrent*pageSize);  
  163.          res = pstate.executeQuery();  
  164.          while(res.next()){  
  165.             CustomerPOJO pojo=newCustomerPOJO(res.getBigDecimal(1),res.getString(2),res.getString( 3),res.getInt(4),res.getString(5),res.getString(6),res.getBigDecimal( 7),res.getInt(8),res.getInt(9));  
  166.             list.add(pojo);  
  167.          }  
  168.       } catch (SQLException e) {  
  169.          e.printStackTrace();  
  170.       }finally{  
  171.          try {  
  172.             res.close();  
  173.             pstate.close();  
  174.          } catch (Exception e2) {  
  175.             e2.printStackTrace();  
  176.          }  
  177.       }  
  178.       return list;  
  179.    }  
  180.    public int findAllCount() {  
  181.       int count = 0;  
  182.       PreparedStatement pstate = null;  
  183.       ResultSet res = null;  
  184.       String sql = "select count(customer_id) fromcustomer" ;  
  185.       try {  
  186.          pstate = this.conn.prepareStatement(sql);  
  187.          res = pstate.executeQuery();  
  188.          while(res.next()){  
  189.             count = res.getInt(1);  
  190.          }  
  191.       } catch (Exception e) {  
  192.          e.printStackTrace();  
  193.       } finally{  
  194.          try {  
  195.             res.close();  
  196.             pstate.close();  
  197.          } catch (Exception e2) {  
  198.             e2.printStackTrace();  
  199.          }  
  200.       }  
  201.       return count;  
  202.    }  
  203. }  
public class CustomerDAOImpl implements CustomerDAO {
   Connection conn;
   public CustomerDAOImpl(Connection conn){
      this.conn = conn;
   }
   public boolean doIns(CustomerPOJO pojo) {
      boolean flag=false;
      PreparedStatement pstate = null;
      try {
         this.conn.setAutoCommit(false);
         String sql="insert into customer(customer_id,customer_name,customer_sex,customer_tel,customer_adress,customer_pro_id)values(DH1.nextval,?,?,?,?,?)";
         pstate = this.conn.prepareStatement(sql);
         pstate.setString(1,pojo.getCustomerName());
         pstate.setInt(2,pojo.getCustomerSex());
         pstate.setString(3,pojo.getCustomerTel());
         pstate.setString(4,pojo.getCustomerAdress());
         pstate.setBigDecimal(5,pojo.getCustomerProId());
         pstate.execute();
         this.conn.commit();
         flag = true;
      } catch (SQLException e) {
         e.printStackTrace();
         try {
            this.conn.rollback();
         } catch (SQLException e1) {
            e1.printStackTrace();
         }
      }finally{
         try {
            pstate.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
      return flag;
   }
 
   public boolean doDel(BigDecimal customerId) {//伪删除,使信息不可见,方便误删找回
      boolean flag = false;
      PreparedStatement pstate = null;
      try {
         this.conn.setAutoCommit(false);
         String sql = "update customer set is_delete=0 wherecustomer_id = ?";
         pstate = this.conn.prepareStatement(sql);
         pstate.setBigDecimal(1, customerId);
         pstate.execute();
         this.conn.commit();
         flag = true;
      } catch (SQLException e) {
         e.printStackTrace();
         try {
            this.conn.rollback();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      } finally{
         try {
            pstate.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      return flag;
   }
 
   public boolean doUpd(CustomerPOJO pojo) {
      boolean flag = false;
      PreparedStatement pstate = null;
      try {
         this.conn.setAutoCommit(false);
         String sql = "update customer setcustomer_name=?,password=?,customer_sex=?,customer_tel=?,customer_adress=?,customer_pro_id=?,is_delete=?,role_mark=?where customer_id=?";
         pstate = this.conn.prepareStatement(sql);
         pstate.setBigDecimal(1,pojo.getCustomerId());
         pstate.setString(2,pojo.getCustomerName());
         pstate.setString(3,pojo.getPassword());
         pstate.setInt(4,pojo.getCustomerSex());
         pstate.setString(5,pojo.getCustomerTel());
         pstate.setString(6, pojo.getCustomerAdress());
         pstate.setBigDecimal(7,pojo.getCustomerProId());
         pstate.setInt(8, pojo.getIsDelete());
         pstate.setInt(9, pojo.getRoleMark());
         pstate.execute();
         this.conn.commit();
         flag = true;
      } catch (Exception e) {
         e.printStackTrace();
         try {
            this.conn.rollback();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      } finally{
         try {
            pstate.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      return flag;
   }
 
   public CustomerPOJO findById(BigDecimal customerId) {
      CustomerPOJO pojo = null;
      PreparedStatement pstate = null;
      ResultSet res = null;
      String sql = "select customer_name, password,customer_sex, customer_tel, customer_adress, customer_pro_id, is_delete, role_markfrom customer where customer_id = ? and is_delete=1";
      try {
         pstate = this.conn.prepareStatement(sql);
         pstate.setBigDecimal(1, customerId);
         res = pstate.executeQuery();
         while(res.next()){
            pojo=new CustomerPOJO(customerId,res.getString(1),res.getString(2),res.getInt(3),res.getString(4),res.getString(5),res.getBigDecimal(6),res.getInt(7),res.getInt(8));
           
         }
      } catch (SQLException e) {
         e.printStackTrace();
      }finally{
         try {
            res.close();
            pstate.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      return pojo;
   }
 
   public CustomerPOJO findByName(String customerName) {
      CustomerPOJO pojo = null;
      PreparedStatement pstate = null;
      ResultSet res = null;
      String sql = "select customer_id, password,customer_sex, customer_tel, customer_adress, customer_pro_id, is_delete,role_mark from customer where customer_name = ? and is_delete=1";
      try {
         pstate = this.conn.prepareStatement(sql);
         pstate.setString(1, customerName);
         res = pstate.executeQuery();
         while(res.next()){
            pojo=newCustomerPOJO(res.getBigDecimal(1),customerName,res.getString(2),res.getInt(3),res.getString(4),res.getString(5),res.getBigDecimal(6),res.getInt(7),res.getInt(8));
           
         }
      } catch (SQLException e) {
         e.printStackTrace();
      }finally{
         try {
            res.close();
            pstate.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      return pojo;
   }
   public List<CustomerPOJO> findAll(int pageSize, int pageCurrent) {
      List<CustomerPOJO> list = new ArrayList<CustomerPOJO>();
      PreparedStatement pstate = null;
      ResultSet res = null;
      String sql = "select customer_id, customer_name,password, customer_sex, customer_tel, customer_adress, customer_pro_id,is_delete, role_mark  from " +
           "(select customer_id, customer_name,password, customer_sex, customer_tel, customer_adress, customer_pro_id,is_delete, role_mark, rownum as rn from customer)  where rn > ? andrn<=? and is_delete = 1" ;
      try {
         pstate = this.conn.prepareStatement(sql);
         pstate.setInt(1,(pageCurrent-1)*pageSize);
         pstate.setInt(2, pageCurrent*pageSize);
         res = pstate.executeQuery();
         while(res.next()){
            CustomerPOJO pojo=newCustomerPOJO(res.getBigDecimal(1),res.getString(2),res.getString(3),res.getInt(4),res.getString(5),res.getString(6),res.getBigDecimal(7),res.getInt(8),res.getInt(9));
            list.add(pojo);
         }
      } catch (SQLException e) {
         e.printStackTrace();
      }finally{
         try {
            res.close();
            pstate.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      return list;
   }
   public int findAllCount() {
      int count = 0;
      PreparedStatement pstate = null;
      ResultSet res = null;
      String sql = "select count(customer_id) fromcustomer" ;
      try {
         pstate = this.conn.prepareStatement(sql);
         res = pstate.executeQuery();
         while(res.next()){
            count = res.getInt(1);
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally{
         try {
            res.close();
            pstate.close();
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      return count;
   }
}

代理类:

  1. public class CustomerDAOProxy implements CustomerDAO {  
  2.     Connection conn = null;  
  3.     CustomerDAOImpl impl=null;  
  4.     public CustomerDAOProxy(){  
  5.         try {  
  6.             this.conn=JDBCHelper.getConn();  
  7.         } catch (Exception e) {  
  8.             e.printStackTrace();  
  9.         }  
  10.         this.impl=new CustomerDAOImpl(this.conn);  
  11.     }  
  12.     public boolean doIns(CustomerPOJO pojo) {  
  13.         boolean flag = this.impl.doIns(pojo);  
  14.         this.close();  
  15.         return flag;  
  16.     }  
  17.     public boolean doDel(BigDecimal customerId) {  
  18.         boolean flag = this.impl.doDel(customerId);  
  19.         this.close();  
  20.         return flag;  
  21.     }  
  22.     public boolean doUpd(CustomerPOJO pojo) {  
  23.         boolean flag = this.impl.doUpd(pojo);  
  24.         this.close();  
  25.         return flag;  
  26.     }  
  27.     public CustomerPOJO findById(BigDecimal cid) {  
  28.         CustomerPOJO pojo = this.impl.findById(cid);  
  29.         this.close();  
  30.         return pojo;  
  31.       
  32.     }  
  33.     public CustomerPOJO findByName(String cname) {  
  34.         CustomerPOJO pojo = this.impl.findByName(cname);  
  35.         this.close();  
  36.         return pojo;  
  37.     }  
  38.     public List<CustomerPOJO> findAll(int pageSize, int pageCurrent) {  
  39.         List<CustomerPOJO> list=this.impl.findAll(pageSize, pageCurrent);  
  40.         this.close();  
  41.         return list;  
  42.     }  
  43.     public int findAllCount() {  
  44.         int count=this.impl.findAllCount();  
  45.         this.close();  
  46.         return count;  
  47.     }  
  48.     public void close(){  
  49.         try {  
  50.             this.conn.close();  
  51.         } catch (Exception e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55.       
  56.   
  57. }  
public class CustomerDAOProxy implements CustomerDAO {
	Connection conn = null;
	CustomerDAOImpl impl=null;
	public CustomerDAOProxy(){
		try {
			this.conn=JDBCHelper.getConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.impl=new CustomerDAOImpl(this.conn);
	}
	public boolean doIns(CustomerPOJO pojo) {
		boolean flag = this.impl.doIns(pojo);
		this.close();
		return flag;
	}
	public boolean doDel(BigDecimal customerId) {
		boolean flag = this.impl.doDel(customerId);
		this.close();
		return flag;
	}
	public boolean doUpd(CustomerPOJO pojo) {
		boolean flag = this.impl.doUpd(pojo);
		this.close();
		return flag;
	}
	public CustomerPOJO findById(BigDecimal cid) {
		CustomerPOJO pojo = this.impl.findById(cid);
		this.close();
		return pojo;
	
	}
	public CustomerPOJO findByName(String cname) {
		CustomerPOJO pojo = this.impl.findByName(cname);
		this.close();
		return pojo;
	}
	public List<CustomerPOJO> findAll(int pageSize, int pageCurrent) {
		List<CustomerPOJO> list=this.impl.findAll(pageSize, pageCurrent);
		this.close();
		return list;
	}
	public int findAllCount() {
		int count=this.impl.findAllCount();
		this.close();
		return count;
	}
	public void close(){
		try {
			this.conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}

工厂类:

  1. public class CustomerDAOFactory {  
  2.     public static CustomerDAO getDAOInstance(){  
  3.         return new CustomerDAOProxy();  
  4.     }  
  5. }  
  6.   
  7.   
  8. public class AddCustomer extends HttpServlet {  
  9.   
  10.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException {  
  12.         request.setCharacterEncoding("utf-8");  
  13.         String cname = request.getParameter("cname");  
  14.         int csex = Integer.parseInt(request.getParameter("csex"));  
  15.         String ctel = request.getParameter("ctel");  
  16.         String cadress = request.getParameter("cadress");  
  17.         BigDecimal cpid = new BigDecimal(request.getParameter("cpid"));  
  18.         CustomerPOJO pojo = new CustomerPOJO(cname,csex,ctel,cadress,cpid);  
  19.         System.out.println("输出数据:"+pojo.toString());  
  20.         boolean flag=CustomerDAOFactory.getDAOInstance().doIns(pojo);  
  21.         PrintWriter out = response.getWriter();  
  22.         StringBuffer sb = new StringBuffer();  
  23.         sb.append(flag);  
  24.         out.print(sb.toString());  
  25.         out.close();  
  26.     }  
  27.   
  28.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  29.             throws ServletException, IOException {  
  30.         this.doGet(request, response);  
  31.     }  
  32.   
  33. }  
public class CustomerDAOFactory {
	public static CustomerDAO getDAOInstance(){
		return new CustomerDAOProxy();
	}
}


public class AddCustomer extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String cname = request.getParameter("cname");
		int csex = Integer.parseInt(request.getParameter("csex"));
		String ctel = request.getParameter("ctel");
		String cadress = request.getParameter("cadress");
		BigDecimal cpid = new BigDecimal(request.getParameter("cpid"));
		CustomerPOJO pojo = new CustomerPOJO(cname,csex,ctel,cadress,cpid);
		System.out.println("输出数据:"+pojo.toString());
		boolean flag=CustomerDAOFactory.getDAOInstance().doIns(pojo);
		PrintWriter out = response.getWriter();
		StringBuffer sb = new StringBuffer();
		sb.append(flag);
		out.print(sb.toString());
		out.close();
	}

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

}


Servlet处理类

 CustomerQuery 客户数据查询:

  1. public class CustomerQuery extends HttpServlet {  
  2.     private static final long serialVersionUID = 1225972715083060475L;  
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         request.setCharacterEncoding("utf-8");  
  6.         response.setCharacterEncoding("utf-8");  
  7.         response.setContentType("text/html; charset=utf-8");  
  8.         if(request.getParameter("customerId")!=null){ //如果ID有输入,显示该ID的客户  
  9.         BigDecimal customerId = new BigDecimal(request.getParameter("customerId"));  
  10.         CustomerPOJO pojo = CustomerDAOFactory.getDAOInstance().findById(customerId);  
  11.         request.setAttribute("pojo", pojo);  
  12.         String str="";  
  13.         if(pojo.getCustomerSex()==1){  
  14.             str="男";  
  15.         }else{  
  16.             str="女";  
  17.         }  
  18.         PrintWriter out = response.getWriter();  
  19.         StringBuffer sb = new StringBuffer();  
  20.         sb.append("<table id='mytable'><tr><th>客户ID</th><th>客户名称</th><th>客户密码</th><th>客户性别</th><th>联系方式</th><th>客户住址</th><th>购买产品id</th><th>操作</th></tr>");  
  21.         sb.append("<tr>" +  
  22.                 "<td>"+pojo.getCustomerId()+"</td>" +  
  23.                 "<td>"+pojo.getCustomerName()+"</td>" +  
  24.                 "<td>"+pojo.getPassword()+"</td>" +  
  25.                 "<td>"+str+"</td>" +  
  26.                 "<td>"+pojo.getCustomerTel()+"</td>" +  
  27.                 "<td>"+pojo.getCustomerAdress()+"</td>" +  
  28.                 "<td>"+pojo.getCustomerProId()+"</td>" +  
  29.                 "<td><a href='#' onclick='goUpdate("+pojo.getCustomerId()+ ")'>修改</a>    <a href='#' onclick='goDelete("+pojo.getCustomerId()+")'>删除</a></td>" +  
  30.                 "</tr>");  
  31.                 sb.append("</table>");  
  32.                 out.print(sb.toString());  
  33.                 out.close();  
  34.         }  
  35.         else if(request.getParameter("customerName")!= null){//如果名字有输入,显示该姓名的客户  
  36.             String customerName = request.getParameter("customerName");  
  37.             CustomerPOJO pojo = CustomerDAOFactory.getDAOInstance().findByName(customerName);  
  38.             request.setAttribute("pojo", pojo);  
  39.             String str="";  
  40.             if(pojo.getCustomerSex()==1){  
  41.                 str="男";  
  42.             }else{  
  43.                 str="女";  
  44.             }  
  45.             PrintWriter out = response.getWriter();  
  46.             StringBuffer sb = new StringBuffer();  
  47.             sb.append("<table id='mytable'><tr><th>客户ID</th><th>客户名称</th><th>客户密码</th><th>客户性别</th><th>联系方式</th><th>客户住址</th><th>购买产品id</th><th>操作</th></tr>");  
  48.             sb.append("<tr>" +  
  49.                     "<td>"+pojo.getCustomerId()+"</td>" +  
  50.                     "<td>"+pojo.getCustomerName()+"</td>" +  
  51.                     "<td>"+pojo.getPassword()+"</td>" +  
  52.                     "<td>"+str+"</td>" +  
  53.                     "<td>"+pojo.getCustomerTel()+"</td>" +  
  54.                     "<td>"+pojo.getCustomerAdress()+"</td>" +  
  55.                     "<td>"+pojo.getCustomerProId()+"</td>" +  
  56.                     "<td><a href='#' onclick='goUpdate("+pojo.getCustomerId()+ ")'>修改</a>    <a href='#' onclick='goDelete("+pojo.getCustomerId()+")'>删除</a></td>" +  
  57.                     "</tr>");  
  58.                     sb.append("</table>");  
  59.                     out.print(sb.toString());  
  60.                     out.close();  
  61.         }else{//ID 和名字都没有输入,显示全部客户列表  
  62.             int pageSize = Integer.parseInt(request.getParameter("pageSize")); //得到一页显示的数据笔数  
  63.             int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent")); //得到要显示哪一页的数据  
  64.             List<CustomerPOJO> list = CustomerDAOFactory.getDAOInstance().findAll(pageSize, pageCurrent);  
  65.             int count=CustomerDAOFactory.getDAOInstance().findAllCount();  
  66.             PrintWriter out = response.getWriter();  
  67.             StringBuffer sb = new StringBuffer();  
  68.             sb.append("<input type='hidden' id='count' value='"+count+"'/>");  
  69.             sb.append("<table id='mytable'><tr><th>客户ID</th><th>客户名称</th><th>客户密码</th><th>客户性别</th><th>联系方式</th><th>客户住址</th><th>购买产品id</th><th>操作</th></tr>");  
  70.             for(CustomerPOJO pojo : list){  
  71.                 String str="";  
  72.                 if(pojo.getCustomerSex()==1){  
  73.                     str="男";  
  74.                 }else{  
  75.                     str="女";  
  76.                 }  
  77.                 sb.append("<tr>" +  
  78.                         "<td>"+pojo.getCustomerId()+ "</td>" +  
  79.                         "<td>"+pojo.getCustomerName()+ "</td>" +  
  80.                         "<td>"+pojo.getPassword()+ "</td>" +  
  81.                         "<td>"+str+"</td>" +  
  82.                         "<td>"+pojo.getCustomerTel()+ "</td>" +  
  83.                         "<td>"+pojo.getCustomerAdress()+ "</td>" +  
  84.                         "<td>"+pojo.getCustomerProId()+ "</td>" +  
  85.                         "<td><a href='#' onclick='goUpdate("+pojo.getCustomerId()+ ")'>修改</a>    <a href='#' onclick='goDelete("+pojo.getCustomerId()+")'>删除</a></td>" +  
  86.                         "</tr>");  
  87.             }  
  88.             sb.append("</table>");  
  89.             sb.append("<input type='button' id='first' value='|<' onclick='query(1)'/>");  
  90.             sb.append("<input type='button' id='up' value='<' onclick='query(2)'/>");  
  91.             sb.append("<input type='button' id='next' value='>' onclick='query(3)'/>");  
  92.             sb.append("<input type='button' id='end' value='>|' onclick='query(4)'/>");  
  93.             sb.append("<span id='showPageMessage'></span>");  
  94.             out.print(sb.toString());  
  95.             out.close();  
  96.         }  
  97.     }  
  98.   
  99.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  100.             throws ServletException, IOException {  
  101.         doGet(request,response);  
  102.     }  
  103. }  
public class CustomerQuery extends HttpServlet {
	private static final long serialVersionUID = 1225972715083060475L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		if(request.getParameter("customerId")!=null){//如果ID有输入,显示该ID的客户
		BigDecimal customerId = new BigDecimal(request.getParameter("customerId"));
		CustomerPOJO pojo = CustomerDAOFactory.getDAOInstance().findById(customerId);
		request.setAttribute("pojo", pojo);
		String str="";
		if(pojo.getCustomerSex()==1){
			str="男";
		}else{
			str="女";
		}
		PrintWriter out = response.getWriter();
		StringBuffer sb = new StringBuffer();
		sb.append("<table id='mytable'><tr><th>客户ID</th><th>客户名称</th><th>客户密码</th><th>客户性别</th><th>联系方式</th><th>客户住址</th><th>购买产品id</th><th>操作</th></tr>");
		sb.append("<tr>" +
				"<td>"+pojo.getCustomerId()+"</td>" +
				"<td>"+pojo.getCustomerName()+"</td>" +
				"<td>"+pojo.getPassword()+"</td>" +
				"<td>"+str+"</td>" +
				"<td>"+pojo.getCustomerTel()+"</td>" +
				"<td>"+pojo.getCustomerAdress()+"</td>" +
				"<td>"+pojo.getCustomerProId()+"</td>" +
				"<td><a href='#' onclick='goUpdate("+pojo.getCustomerId()+")'>修改</a>    <a href='#' onclick='goDelete("+pojo.getCustomerId()+")'>删除</a></td>" +
				"</tr>");
				sb.append("</table>");
				out.print(sb.toString());
				out.close();
		}
		else if(request.getParameter("customerName")!=null){//如果名字有输入,显示该姓名的客户
			String customerName = request.getParameter("customerName");
			CustomerPOJO pojo = CustomerDAOFactory.getDAOInstance().findByName(customerName);
			request.setAttribute("pojo", pojo);
			String str="";
			if(pojo.getCustomerSex()==1){
				str="男";
			}else{
				str="女";
			}
			PrintWriter out = response.getWriter();
			StringBuffer sb = new StringBuffer();
			sb.append("<table id='mytable'><tr><th>客户ID</th><th>客户名称</th><th>客户密码</th><th>客户性别</th><th>联系方式</th><th>客户住址</th><th>购买产品id</th><th>操作</th></tr>");
			sb.append("<tr>" +
					"<td>"+pojo.getCustomerId()+"</td>" +
					"<td>"+pojo.getCustomerName()+"</td>" +
					"<td>"+pojo.getPassword()+"</td>" +
					"<td>"+str+"</td>" +
					"<td>"+pojo.getCustomerTel()+"</td>" +
					"<td>"+pojo.getCustomerAdress()+"</td>" +
					"<td>"+pojo.getCustomerProId()+"</td>" +
					"<td><a href='#' onclick='goUpdate("+pojo.getCustomerId()+")'>修改</a>    <a href='#' onclick='goDelete("+pojo.getCustomerId()+")'>删除</a></td>" +
					"</tr>");
					sb.append("</table>");
					out.print(sb.toString());
					out.close();
		}else{//ID 和名字都没有输入,显示全部客户列表
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));//得到一页显示的数据笔数
			int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));//得到要显示哪一页的数据
			List<CustomerPOJO> list = CustomerDAOFactory.getDAOInstance().findAll(pageSize, pageCurrent);
			int count=CustomerDAOFactory.getDAOInstance().findAllCount();
			PrintWriter out = response.getWriter();
			StringBuffer sb = new StringBuffer();
			sb.append("<input type='hidden' id='count' value='"+count+"'/>");
			sb.append("<table id='mytable'><tr><th>客户ID</th><th>客户名称</th><th>客户密码</th><th>客户性别</th><th>联系方式</th><th>客户住址</th><th>购买产品id</th><th>操作</th></tr>");
			for(CustomerPOJO pojo : list){
				String str="";
				if(pojo.getCustomerSex()==1){
					str="男";
				}else{
					str="女";
				}
				sb.append("<tr>" +
						"<td>"+pojo.getCustomerId()+"</td>" +
						"<td>"+pojo.getCustomerName()+"</td>" +
						"<td>"+pojo.getPassword()+"</td>" +
						"<td>"+str+"</td>" +
						"<td>"+pojo.getCustomerTel()+"</td>" +
						"<td>"+pojo.getCustomerAdress()+"</td>" +
						"<td>"+pojo.getCustomerProId()+"</td>" +
						"<td><a href='#' onclick='goUpdate("+pojo.getCustomerId()+")'>修改</a>    <a href='#' onclick='goDelete("+pojo.getCustomerId()+")'>删除</a></td>" +
						"</tr>");
			}
			sb.append("</table>");
			sb.append("<input type='button' id='first' value='|<' onclick='query(1)'/>");
			sb.append("<input type='button' id='up' value='<' onclick='query(2)'/>");
			sb.append("<input type='button' id='next' value='>' onclick='query(3)'/>");
			sb.append("<input type='button' id='end' value='>|' onclick='query(4)'/>");
			sb.append("<span id='showPageMessage'></span>");
			out.print(sb.toString());
			out.close();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
CustomerUpd   客户数据更新:
  1. public class CustomerUpd extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         request.setCharacterEncoding("utf-8");  
  6.         BigDecimal customerId = new BigDecimal(request.getParameter("cid"));  
  7.         String customerName = request.getParameter("cname");  
  8.         String password = request.getParameter("cpassword");  
  9.         int customerSex = Integer.parseInt(request.getParameter("csex"));  
  10.         String customerTel = request.getParameter("ctel");  
  11.         String customerAdress = request.getParameter("cadress");  
  12.         BigDecimal customerProId = new BigDecimal(request.getParameter("cpid"));  
  13.         int isDelete=Integer.parseInt(request.getParameter("isDelete"));  
  14.         int roleMark=Integer.parseInt(request.getParameter("roleMark"));  
  15.         CustomerPOJO pojo = new CustomerPOJO(customerId, customerName,password,customerSex,customerTel,customerAdress,customerProId, isDelete, roleMark);  
  16.         System.out.println("输出数据:"+pojo.toString());  
  17.         boolean flag = CustomerDAOFactory.getDAOInstance().doUpd(pojo);  
  18.         PrintWriter out = response.getWriter();  
  19.         StringBuffer sb = new StringBuffer();  
  20.         sb.append(flag);  
  21.         out.print(sb.toString());  
  22.         out.close();  
  23.     }  
  24.   
  25.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  26.             throws ServletException, IOException {  
  27.             doGet(request, response);  
  28.     }  
  29.   
  30. }  
public class CustomerUpd extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		BigDecimal customerId = new BigDecimal(request.getParameter("cid"));
		String customerName = request.getParameter("cname");
		String password = request.getParameter("cpassword");
		int customerSex = Integer.parseInt(request.getParameter("csex"));
		String customerTel = request.getParameter("ctel");
		String customerAdress = request.getParameter("cadress");
		BigDecimal customerProId = new BigDecimal(request.getParameter("cpid"));
		int isDelete=Integer.parseInt(request.getParameter("isDelete"));
		int roleMark=Integer.parseInt(request.getParameter("roleMark"));
		CustomerPOJO pojo = new CustomerPOJO(customerId, customerName,password,customerSex,customerTel,customerAdress,customerProId, isDelete, roleMark);
		System.out.println("输出数据:"+pojo.toString());
		boolean flag = CustomerDAOFactory.getDAOInstance().doUpd(pojo);
		PrintWriter out = response.getWriter();
		StringBuffer sb = new StringBuffer();
		sb.append(flag);
		out.print(sb.toString());
		out.close();
	}

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

}

DelCustomer   客户数据删除(伪删除,使数据不可见,方便误删找回)

  1. public class DelCustomer extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         PrintWriter out = response.getWriter();  
  6.         BigDecimal customerId = new BigDecimal(request.getParameter("customerId"));  
  7.         boolean flag = CustomerDAOFactory.getDAOInstance().doDel(customerId);  
  8.         System.out.println(flag);  
  9.         out.print(flag);  
  10.         out.close();  
  11.     }  
  12.   
  13.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.         this.doGet(request, response);  
  16.     }  
  17.   
  18. }  
public class DelCustomer extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		BigDecimal customerId = new BigDecimal(request.getParameter("customerId"));
		boolean flag = CustomerDAOFactory.getDAOInstance().doDel(customerId);
		System.out.println(flag);
		out.print(flag);
		out.close();
	}

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

}

FindCustomerById  查询客户,用于更新用户信息

  1. public class FindCustomerById extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         this.doPost(request, response);  
  6.   
  7.     }  
  8.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  9.             throws ServletException, IOException {  
  10.         request.setCharacterEncoding("utf-8");  
  11.         BigDecimal cID = new BigDecimal(request.getParameter("cid"));  
  12.         CustomerPOJO pojo = CustomerDAOFactory.getDAOInstance().findById(cID);  
  13.         request.setAttribute("pojo", pojo);  
  14.         request.getRequestDispatcher("/manager/updateCustomer.jsp").forward(request, response);  
  15.           
  16.     }  
  17. }  
public class FindCustomerById extends HttpServlet {

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

	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		BigDecimal cID = new BigDecimal(request.getParameter("cid"));
		CustomerPOJO pojo = CustomerDAOFactory.getDAOInstance().findById(cID);
		request.setAttribute("pojo", pojo);
		request.getRequestDispatcher("/manager/updateCustomer.jsp").forward(request, response);
		
	}
}


JDBCHelper 数据库辅助:

  1. public class JDBCHelper {  
  2.     public static final String DRIVER =  "oracle.jdbc.driver.OracleDriver";  
  3.     public static final String URL =  "jdbc:oracle:thin:@localhost:1521:xxx";  
  4.     public static final String DBNAME =  "name";  
  5.     public static final String PASSWORD =  "xddd";  
  6.     public static Connection getConn() throws Exception{  
  7.         Class.forName(DRIVER);  
  8.         Connection conn = DriverManager.getConnection(URL, DBNAME, PASSWORD);  
  9.         return conn;  
  10.           
  11.           
  12.     }  
  13. }  
public class JDBCHelper {
	public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String URL = "jdbc:oracle:thin:@localhost:1521:xxx";
	public static final String DBNAME = "name";
	public static final String PASSWORD = "xddd";
	public static Connection getConn() throws Exception{
		Class.forName(DRIVER);
		Connection conn = DriverManager.getConnection(URL, DBNAME, PASSWORD);
		return conn;
		
		
	}
}


AddCustomer.jsp   新增客户页面

  1. <%@ page language="java" import="java.util.*" pageEncoding= "UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. %>  
  5.   
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <title>新增客户</title>  
  10.     <script type="text/javascript" src="<%=path%>/js/jquery-1.8.3.js"></script>  
  11.   </head>  
  12.     
  13.   <body>  
  14.     <center>  
  15.         <h2>新增客户</h2>  
  16.         <hr/>  
  17.         <form name = "cus">  
  18.             客户名称:<input type="text" name="cname"/>  
  19.             <br/>  
  20.             <a style="float:left">客户性别:</a><select name="csex">  
  21.                         <option value="0" selected= "selected">女</option>  
  22.                         <option value="1">男</option>  
  23.                     </select>  
  24.             <br/>  
  25.             联系方式:<input type="text" name="ctel"/>  
  26.             <br/>  
  27.             客户住址:<input type="text" name="cadress"/>  
  28.             <br/>  
  29.             购买产品id:<input type="text" name="cpid"/>  
  30.             <br/>  
  31.             <input type="button" value="确认" onclick= "add()"/>  
  32.             <input type="reset" value="重置" />  
  33.             <input type="button" value="返回" onclick= "back()"/>  
  34.         </form>  
  35.     </center>   
  36.   </body>  
  37.   <script type="text/javascript">  
  38.         function add(){  
  39.             var cname=cus.cname.value;  
  40.             var csex=cus.csex.value;  
  41.             var ctel=cus.ctel.value;  
  42.             var cadress=cus.cadress.value;  
  43.             var cpid=cus.cpid.value;  
  44.              $(document).ready(function(){  
  45.                 //设置提交的路径,和参数  
  46.                 $.post("<%=path%>/AddCustomer",  
  47.                 {"cname":cname,"csex":csex,"ctel":ctel, "cadress":cadress,"cpid":cpid},  
  48.                 function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容  
  49.                     if(data == "true"){  
  50.                         alert("客户新增成功");  
  51.                         back();  
  52.                     }else{  
  53.                         alert("客户新增失败,请联系系统管理员");  
  54.                     }  
  55.                 });  
  56.             });  
  57.         }  
  58.               
  59.         function back(){  
  60.             opener.location.reload();   
  61.             //window.dialogArguments.query(0);//刷新之前页面   
  62.             window.close();//关闭当前页面  
  63.         }  
  64.     </script>  
  65. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>新增客户</title>
    <script type="text/javascript" src="<%=path%>/js/jquery-1.8.3.js"></script>
  </head>
  
  <body>
	<center>
		<h2>新增客户</h2>
		<hr/>
		<form name = "cus">
			客户名称:<input type="text" name="cname"/>
			<br/>
			<a style="float:left">客户性别:</a><select name="csex">
						<option value="0" selected="selected">女</option>
						<option value="1">男</option>
					</select>
			<br/>
			联系方式:<input type="text" name="ctel"/>
			<br/>
			客户住址:<input type="text" name="cadress"/>
			<br/>
			购买产品id:<input type="text" name="cpid"/>
			<br/>
			<input type="button" value="确认" onclick="add()"/>
			<input type="reset" value="重置" />
			<input type="button" value="返回" onclick="back()"/>
		</form>
	</center>	
  </body>
  <script type="text/javascript">
		function add(){
			var cname=cus.cname.value;
			var csex=cus.csex.value;
			var ctel=cus.ctel.value;
			var cadress=cus.cadress.value;
			var cpid=cus.cpid.value;
			 $(document).ready(function(){
			 	//设置提交的路径,和参数
				$.post("<%=path%>/AddCustomer",
				{"cname":cname,"csex":csex,"ctel":ctel,"cadress":cadress,"cpid":cpid},
				function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
	 				if(data == "true"){
	 					alert("客户新增成功");
	 					back();
	 				}else{
	 					alert("客户新增失败,请联系系统管理员");
	 				}
	 			});
			});
		}
			
		function back(){
			opener.location.reload(); 
			//window.dialogArguments.query(0);//刷新之前页面 
			window.close();//关闭当前页面
		}
	</script>
</html>

queryCustomer.jsp   客户查询删除页面:

  1. <%@ page language="java" import="java.util.*" pageEncoding= "UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. %>  
  5.   
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <title>查询客户</title>  
  10.     <script type="text/javascript" src="<%=path%>/js/jquery-1.8.3.js"></script>  
  11.   </head>  
  12.     
  13.   <body>  
  14.     <form name = "que">  
  15.         <fieldset title="查询">  
  16.             <legend>  
  17.                 <span width="12%" height="25"  class="STYLE1"  
  18.                                 style="color: black;">查询条件</span>  
  19.             </legend>  
  20.             <input type="text" name="customer"/>  
  21.             <input type="button" value="查询客户ID" onclick= "queryById(0)"/>  
  22.             <input type="button" value="查询客户姓名" onclick= "queryByName(0)"/>  
  23.             <input type="button" value="显示所有客户资料" onclick= "query(0)"/>  
  24.             <input type="button" value="新增" onclick= "goAdd()"/>  
  25.         </fieldset>  
  26.               
  27.         </form>  
  28.         <div id="showTable"></div>  
  29.         </body>  
  30.         <script type="text/javascript">  
  31.         var pageSize = 10;//一页显示的数据笔数  
  32.         var pageCurrent = 1;//显示的页数  
  33.         var allCount = 0;//总共的数据笔数  
  34.         var allPage = 0;//总共数据页数  
  35.         query(0);  
  36.         function query(num){  
  37.             if(num == 1){//第一页  
  38.                 pageCurrent = 1;  
  39.             }else if(num == 2){ //上一页  
  40.                 pageCurrent = pageCurrent -1;  
  41.             }else if(num == 3){ //下一页  
  42.                 pageCurrent = pageCurrent + 1;  
  43.             }else if(num == 4){ //最后一页  
  44.                 pageCurrent = allPage;  
  45.             }  
  46.              $(document).ready(function(){  
  47.                 //设置提交的路径,和参数  
  48.                 $.post("<%=path%>/CustomerQuery",{"pageSize":pageSize, "pageCurrent":pageCurrent},  
  49.                 function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容  
  50.                     $("#showTable").html(data);//显示Servlet返回的内容  
  51.                     controlButton();  
  52.                 });  
  53.             });  
  54.         }  
  55.         function controlButton(){//设置按钮可见与否,停在第一页时不能点击上一页。停在最后一页时,不能点击下一页  
  56.             allCount = $("#count").val();  
  57.             if(allCount%pageSize == 0){  
  58.                 allPage = allCount/pageSize  
  59.             }else{  
  60.                 allPage = Math.floor(allCount/pageSize) +1;  
  61.             }  
  62.             document.getElementById("first").disabled = false;  
  63.             document.getElementById("up").disabled = false;  
  64.             document.getElementById("next").disabled = false;  
  65.             document.getElementById("end").disabled = false;  
  66.             if(allPage == 1){  
  67.                 document.getElementById("first").disabled = true;  
  68.                 document.getElementById("up").disabled = true;  
  69.                 document.getElementById("next").disabled = true;  
  70.                 document.getElementById("end").disabled = true;  
  71.             }else if(pageCurrent == 1){  
  72.                 document.getElementById("first").disabled = true;  
  73.                 document.getElementById("up").disabled = true;  
  74.             }else if(pageCurrent == allPage){  
  75.                 document.getElementById("next").disabled = true;  
  76.                 document.getElementById("end").disabled = true;  
  77.             }  
  78.             $("#showPageMessage").html("总共"+allCount+"笔数据,当前显示"+pageCurrent+ "页,共"+ allPage+"页");  
  79.               
  80.         }  
  81.             function goAdd(){  
  82.                 var width = window.screen.width ;  
  83.                 var height = window.screen.height ;  
  84.                 window.open("<%=path%>/manager/addCustomer.jsp","新增客户", 'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/ 2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');  
  85.             }  
  86.             function queryById(){  
  87.                 var customerId = que.customer.value;  
  88.                  $(document).ready(function(){  
  89.                     //设置提交的路径,和参数  
  90.                     $.post("<%=path%>/CustomerQuery",{"customerId":customerId},  
  91.                     function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容  
  92.                         $("#showTable").html(data); //显示Servlet返回的内容  
  93.                     });  
  94.                 });  
  95.             }  
  96.             function queryByName(){  
  97.                 var customerName = que.customer.value;  
  98.                  $(document).ready(function(){  
  99.                     //设置提交的路径,和参数  
  100.                     $.post("<%=path%>/CustomerQuery",{"customerName":customerName},  
  101.                     function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容  
  102.                         $("#showTable").html(data); //显示Servlet返回的内容  
  103.                     });  
  104.                 });  
  105.             }  
  106.             function goUpdate(customerId){  
  107.                 var width = window.screen.width ;  
  108.                 var height = window.screen.height ;  
  109.                 window.open("<%=path%>/FindCustomerById?cid="+customerId,"修改客户", 'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/ 2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');  
  110.             }  
  111.             function goDelete(customerId){  
  112.                 if(confirm("确认删除?")){  
  113.                      $(document).ready(function(){  
  114.                     //设置提交的路径,和参数  
  115.                     $.post("<%=path%>/DelCustomer",{"customerId":customerId},  
  116.                     function(data){  
  117.                         //Servlet执行完之后执行方法,data表示的servlet返回数据内容  
  118.                         if(data ==  "true"){  
  119.                             alert("删除成功");  
  120.                             query(0);  
  121.                         }else{  
  122.                             alert("删除失败,请联系系统管理员");  
  123.                         }  
  124.                     });  
  125.                 });  
  126.                 }  
  127.             }  
  128.         </script>  
  129. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>查询客户</title>
    <script type="text/javascript" src="<%=path%>/js/jquery-1.8.3.js"></script>
  </head>
  
  <body>
	<form name = "que">
		<fieldset title="查询">
			<legend>
				<span width="12%" height="25" class="STYLE1"
								style="color: black;">查询条件</span>
			</legend>
			<input type="text" name="customer"/>
			<input type="button" value="查询客户ID" onclick="queryById(0)"/>
			<input type="button" value="查询客户姓名" onclick="queryByName(0)"/>
			<input type="button" value="显示所有客户资料" onclick="query(0)"/>
			<input type="button" value="新增" onclick="goAdd()"/>
		</fieldset>
			
		</form>
		<div id="showTable"></div>
		</body>
		<script type="text/javascript">
		var pageSize = 10;//一页显示的数据笔数
		var pageCurrent = 1;//显示的页数
		var allCount = 0;//总共的数据笔数
		var allPage = 0;//总共数据页数
		query(0);
		function query(num){
			if(num == 1){//第一页
				pageCurrent = 1;
			}else if(num == 2){//上一页
				pageCurrent = pageCurrent -1;
			}else if(num == 3){//下一页
				pageCurrent = pageCurrent + 1;
			}else if(num == 4){//最后一页
				pageCurrent = allPage;
			}
			 $(document).ready(function(){
			 	//设置提交的路径,和参数
				$.post("<%=path%>/CustomerQuery",{"pageSize":pageSize,"pageCurrent":pageCurrent},
				function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
	 				$("#showTable").html(data);//显示Servlet返回的内容
	 				controlButton();
	 			});
			});
		}
		function controlButton(){//设置按钮可见与否,停在第一页时不能点击上一页。停在最后一页时,不能点击下一页
			allCount = $("#count").val();
			if(allCount%pageSize == 0){
				allPage = allCount/pageSize
			}else{
				allPage = Math.floor(allCount/pageSize) +1;
			}
			document.getElementById("first").disabled = false;
			document.getElementById("up").disabled = false;
			document.getElementById("next").disabled = false;
			document.getElementById("end").disabled = false;
			if(allPage == 1){
				document.getElementById("first").disabled = true;
				document.getElementById("up").disabled = true;
				document.getElementById("next").disabled = true;
				document.getElementById("end").disabled = true;
			}else if(pageCurrent == 1){
				document.getElementById("first").disabled = true;
				document.getElementById("up").disabled = true;
			}else if(pageCurrent == allPage){
				document.getElementById("next").disabled = true;
				document.getElementById("end").disabled = true;
			}
			$("#showPageMessage").html("总共"+allCount+"笔数据,当前显示"+pageCurrent+"页,共"+ allPage+"页");
			
		}
			function goAdd(){
				var width = window.screen.width ;
				var height = window.screen.height ;
				window.open("<%=path%>/manager/addCustomer.jsp","新增客户",'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
			}
			function queryById(){
				var customerId = que.customer.value;
				 $(document).ready(function(){
				 	//设置提交的路径,和参数
					$.post("<%=path%>/CustomerQuery",{"customerId":customerId},
					function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
		 				$("#showTable").html(data);//显示Servlet返回的内容
		 			});
				});
			}
			function queryByName(){
				var customerName = que.customer.value;
				 $(document).ready(function(){
				 	//设置提交的路径,和参数
					$.post("<%=path%>/CustomerQuery",{"customerName":customerName},
					function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
		 				$("#showTable").html(data);//显示Servlet返回的内容
		 			});
				});
			}
			function goUpdate(customerId){
				var width = window.screen.width ;
				var height = window.screen.height ;
				window.open("<%=path%>/FindCustomerById?cid="+customerId,"修改客户",'height=400,width=300,top='+(height-450)/2+',left='+(width-300)/2+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
			}
			function goDelete(customerId){
				if(confirm("确认删除?")){
					 $(document).ready(function(){
				 	//设置提交的路径,和参数
					$.post("<%=path%>/DelCustomer",{"customerId":customerId},
					function(data){
						//Servlet执行完之后执行方法,data表示的servlet返回数据内容
		 				if(data == "true"){
		 					alert("删除成功");
		 					query(0);
		 				}else{
		 					alert("删除失败,请联系系统管理员");
		 				}
		 			});
				});
				}
			}
		</script>
</html>

updateCustomer.jsp  更新客户信息界面:

  1. <%@page import="org.jvsun.pojo.CustomerPOJO"%>  
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <title>更新客户</title>  
  11.     <script type="text/javascript" src="<%=path%>/js/jquery-1.8.3.js"></script>  
  12.   </head>  
  13.     
  14.   <body><%--  
  15.   <%  
  16.     CustomerPOJO pojo=new CustomerPOJO();  
  17.     int sex=pojo.getCustomerSex();  
  18.     System.out.println(sex);  
  19.     String str="";  
  20.     if(sex==1){  
  21.         str="男";  
  22.     }else{  
  23.         str="女";  
  24.     }  
  25.   %>  
  26.     --%><center>  
  27.         <h2>更新客户</h2>  
  28.         <hr/>  
  29.         <form name = "cus">  
  30.             <input type="hidden" name="cid" value =  "${pojo.customerId}"/>  
  31.             <br/>  
  32.             客户名称:<input type="text" name="cname" value =  "${pojo.customerName}"/>  
  33.             <br/>  
  34.             客户密码:<input type="text" name="cpassword" value =  "${pojo.password}"/>  
  35.             <br/>  
  36.             客户性别:<input type="text" name="csex" value =  "${pojo.customerSex}"/>  
  37.             <br/>  
  38.             联系方式:<input type="text" name="ctel" value =  "${pojo.customerTel}"/>  
  39.             <br/>  
  40.             客户住址:<input type="text" name="cadress" value =  "${pojo.customerAdress}"/>  
  41.             <br/>  
  42.             购买产品id:<input type="text" name="cpid" value =  "${pojo.customerProId}"/>  
  43.             <br/>  
  44.             客户状态:<input type="text" name="isDelete" value =  "${pojo.isDelete}"/>  
  45.             <br/>  
  46.             客户标识:<input type="text" name="roleMark" value =  "${pojo.roleMark}"/>  
  47.             <br/>  
  48.             <input type="button" value="确认" onclick= "upd()"/>  
  49.             <input type="button" value="返回" onclick= "back()"/>  
  50.         </form>  
  51.     </center>   
  52.   </body>  
  53.   <script type="text/javascript">  
  54.         function upd(){  
  55.             var cid=cus.cid.value;  
  56.             var cname=cus.cname.value;  
  57.             var cpassword=cus.cpassword.value;  
  58.             var csex=cus.csex.value;  
  59.             var ctel=cus.ctel.value;  
  60.             var cadress=cus.cadress.value;  
  61.             var cpid=cus.cpid.value;  
  62.             var isDelete=cus.isDelete.value;  
  63.             var roleMark=cus.roleMark.value;  
  64.              $(document).ready(function(){  
  65.                 //设置提交的路径,和参数  
  66.                 $.post("<%=path%>/CustomerUpd",  
  67.                 {"cid":cid,"cname":cname,"cpassword":cpassword, "csex":csex,"ctel":ctel,"cadress":cadress,"cpid":cpid,"isDelete":isDelete, "roleMark":roleMark},  
  68.                 function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容  
  69.                     if(data == "true"){  
  70.                         alert("客户修改成功");  
  71.                         back();  
  72.                     }else{  
  73.                         alert("客户修改失败,请联系系统管理员");  
  74.                     }  
  75.                 });  
  76.             });  
  77.         }  
  78.               
  79.         function back(){  
  80.             opener.location.reload();   
  81.             //window.dialogArguments.query(0);//刷新之前页面   
  82.             window.close();//关闭当前页面  
  83.         }  
  84.     </script>  
  85. </html>  
<%@page import="org.jvsun.pojo.CustomerPOJO"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>更新客户</title>
    <script type="text/javascript" src="<%=path%>/js/jquery-1.8.3.js"></script>
  </head>
  
  <body><%--
  <%
  	CustomerPOJO pojo=new CustomerPOJO();
  	int sex=pojo.getCustomerSex();
  	System.out.println(sex);
  	String str="";
  	if(sex==1){
  		str="男";
  	}else{
  		str="女";
  	}
  %>
	--%><center>
		<h2>更新客户</h2>
		<hr/>
		<form name = "cus">
			<input type="hidden" name="cid" value = "${pojo.customerId}"/>
			<br/>
			客户名称:<input type="text" name="cname" value = "${pojo.customerName}"/>
			<br/>
			客户密码:<input type="text" name="cpassword" value = "${pojo.password}"/>
			<br/>
			客户性别:<input type="text" name="csex" value = "${pojo.customerSex}"/>
			<br/>
			联系方式:<input type="text" name="ctel" value = "${pojo.customerTel}"/>
			<br/>
			客户住址:<input type="text" name="cadress" value = "${pojo.customerAdress}"/>
			<br/>
			购买产品id:<input type="text" name="cpid" value = "${pojo.customerProId}"/>
			<br/>
			客户状态:<input type="text" name="isDelete" value = "${pojo.isDelete}"/>
			<br/>
			客户标识:<input type="text" name="roleMark" value = "${pojo.roleMark}"/>
			<br/>
			<input type="button" value="确认" onclick="upd()"/>
			<input type="button" value="返回" onclick="back()"/>
		</form>
	</center>	
  </body>
  <script type="text/javascript">
		function upd(){
			var cid=cus.cid.value;
			var cname=cus.cname.value;
			var cpassword=cus.cpassword.value;
			var csex=cus.csex.value;
			var ctel=cus.ctel.value;
			var cadress=cus.cadress.value;
			var cpid=cus.cpid.value;
			var isDelete=cus.isDelete.value;
			var roleMark=cus.roleMark.value;
			 $(document).ready(function(){
			 	//设置提交的路径,和参数
				$.post("<%=path%>/CustomerUpd",
				{"cid":cid,"cname":cname,"cpassword":cpassword,"csex":csex,"ctel":ctel,"cadress":cadress,"cpid":cpid,"isDelete":isDelete,"roleMark":roleMark},
				function(data){//Servlet执行完之后执行方法,data表示的servlet返回数据内容
	 				if(data == "true"){
	 					alert("客户修改成功");
	 					back();
	 				}else{
	 					alert("客户修改失败,请联系系统管理员");
	 				}
	 			});
			});
		}
			
		function back(){
			opener.location.reload(); 
			//window.dialogArguments.query(0);//刷新之前页面 
			window.close();//关闭当前页面
		}
	</script>
</html>

web.xml配置:

  1. <?xml version="1.0" encoding= "UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <display-name></display-name>  
  8.   <servlet>  
  9.     <servlet-name>AddCustomer</servlet-name >  
  10.     <servlet-class>org.jvsun.servlet.AddCustomer</servlet-class >  
  11.   </servlet>  
  12.   <servlet>  
  13.     <servlet-name>CustomerQuery</servlet-name >  
  14.     <servlet-class>org.jvsun.servlet.CustomerQuery</servlet-class >  
  15.   </servlet>  
  16.   <servlet>  
  17.     <servlet-name>CustomerUpd</servlet-name >  
  18.     <servlet-class>org.jvsun.servlet.CustomerUpd</servlet-class >  
  19.   </servlet>  
  20.   <servlet>  
  21.     <servlet-name>DelCustomer</servlet-name >  
  22.     <servlet-class>org.jvsun.servlet.DelCustomer</servlet-class >  
  23.   </servlet>  
  24.   <servlet>  
  25.     <servlet-name>FindCustomerById</servlet-name >  
  26.     <servlet-class>org.jvsun.servlet.FindCustomerById</servlet-class >  
  27.   </servlet>  
  28.   
  29.   <servlet-mapping>  
  30.     <servlet-name>AddCustomer</servlet-name >  
  31.     <url-pattern>/AddCustomer</url-pattern >  
  32.   </servlet-mapping>  
  33.   <servlet-mapping>  
  34.     <servlet-name>CustomerQuery</servlet-name >  
  35.     <url-pattern>/CustomerQuery</url-pattern >  
  36.   </servlet-mapping>  
  37.   <servlet-mapping>  
  38.     <servlet-name>CustomerUpd</servlet-name >  
  39.     <url-pattern>/CustomerUpd</url-pattern >  
  40.   </servlet-mapping>  
  41.   <servlet-mapping>  
  42.     <servlet-name>DelCustomer</servlet-name >  
  43.     <url-pattern>/DelCustomer</url-pattern >  
  44.   </servlet-mapping>  
  45.   <servlet-mapping>  
  46.     <servlet-name>FindCustomerById</servlet-name >  
  47.     <url-pattern>/FindCustomerById</url-pattern >  
  48.   </servlet-mapping>  
  49. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>AddCustomer</servlet-name>
    <servlet-class>org.jvsun.servlet.AddCustomer</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>CustomerQuery</servlet-name>
    <servlet-class>org.jvsun.servlet.CustomerQuery</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>CustomerUpd</servlet-name>
    <servlet-class>org.jvsun.servlet.CustomerUpd</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>DelCustomer</servlet-name>
    <servlet-class>org.jvsun.servlet.DelCustomer</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>FindCustomerById</servlet-name>
    <servlet-class>org.jvsun.servlet.FindCustomerById</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AddCustomer</servlet-name>
    <url-pattern>/AddCustomer</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>CustomerQuery</servlet-name>
    <url-pattern>/CustomerQuery</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>CustomerUpd</servlet-name>
    <url-pattern>/CustomerUpd</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>DelCustomer</servlet-name>
    <url-pattern>/DelCustomer</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>FindCustomerById</servlet-name>
    <url-pattern>/FindCustomerById</url-pattern>
  </servlet-mapping>
</web-app>

好了,所有的代码都展示完了,说这么多 就想知道谁有好的分页工具类能分享一下嘛,以后就能偷个懒了再见