使用说明:
jdk:1.8
tomcat:1.9
数据库:mysql
资源下载链接:http://download.csdn.net/download/yuehailin/10205004
本篇博客使用servlet进行增删改查,c3p0连接数据库,下面是页面的效果展示。
主界面:
查询用户界面:
高级查询(模糊查询):
需要使用的jar包:
博客结构截图:
c3p0-config.xml
jdbc:mysql://localhost:3306/customer
com.mysql.jdbc.Driver
root
123
3
10
2
10
CustomerDao.java
package dao;
import cn.itcast.jdbc.TxQueryRunner;
import domain.Customer;
import domain.PageBean;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.util.ArrayList;
import java.util.List;
public class CustomerDao
{
private QueryRunner qr=new TxQueryRunner();
public void add(Customer c)
{
try {
String sql = "insert into t_customer values(?,?,?,?,?,?)";
Object[] params = {c.getId(), c.getName(), c.getGender(),
c.getPhone(), c.getEmail(), c.getDescription()};
qr.update(sql, params);
}catch (Exception e)
{
throw new RuntimeException(e);
}
}
// public List findAll()
// {
// try{
// String sql="select * from t_customer";
// return qr.query(sql,new BeanListHandler(Customer.class));
// }catch (Exception e)
// {
// throw new RuntimeException(e);
// }
// }
public PageBean findAll(int pc, int pr)
{
try{
/*
*1.他需要创建pageBean对象pb
* 2.设置pb的pc和pr
* 3.得到tr,设置给pb
* 4.得到beanList设置给pb
* 最后返回给pb
*/
PageBean pb=new PageBean<>();
pb.setPc(pc);
pb.setPr(pr);
String sql="select count(*) from t_customer";
Number number=(Number) qr.query(sql,new ScalarHandler<>());
int tr=number.intValue();
pb.setTr(tr);
sql="select * from t_customer order by name limit ?,?";
Object[] params={(pc-1)*pr,pr};
List beanList=qr.query(sql,new BeanListHandler<>(Customer.class),params);
pb.setBeanList(beanList);
return pb;
}catch (Exception e)
{
throw new RuntimeException(e);
}
}
public Customer find(String id)
{
try {
String sql = "select * from t_customer where id=?";
return qr.query(sql, new BeanHandler(Customer.class), id);
}catch (Exception e)
{
throw new RuntimeException(e);
}
}
public void edit(Customer customer)
{
try{
String sql="update t_customer set name=?,gender=?,phone=?,email=?,description=? where id=?";
Object[] params={customer.getName(),customer.getGender(),customer.getPhone(),customer.getEmail(),customer.getDescription(),customer.getId()};
qr.update(sql,params);
}catch (Exception e)
{
throw new RuntimeException(e);
}
}
public void delete(String id)
{
try {
String sql = "delete from t_customer where id=?";
qr.update(sql, id);
}catch (Exception e)
{
throw new RuntimeException(e);
}
}
// public List query(Customer customer) {
//
// try {
// StringBuilder sql = new StringBuilder("select * from t_customer where 1=1 ");
// List
Customer
package domain;
public class Customer
{
private String id;
private String name;
private String gender;
private String phone;
private String email;
private String description;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
PageBean
package domain;
import java.util.List;
public class PageBean
{
private int pc;//当前页码page code
//private int tp;//总页数total pages
private int tr;//总纪录数tatal records
private int pr;//每页纪录数page records
private List beanList;//当前页的纪录
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPc() {
return pc;
}
public void setPc(int pc) {
this.pc = pc;
}
public int getTp()
{
int tp=tr/pr;
return tr % pr == 0 ? tp : tp + 1 ;
}
public int getTr() {
return tr;
}
public void setTr(int tr) {
this.tr = tr;
}
public int getPr() {
return pr;
}
public void setPr(int pr) {
this.pr = pr;
}
public List getBeanList() {
return beanList;
}
public void setBeanList(List beanList) {
this.beanList = beanList;
}
}
CustomerServlet
package servlet;
import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import domain.Customer;
import domain.PageBean;
import service.CustomerService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class CustomerServlet extends BaseServlet {
private CustomerService customerService = new CustomerService();
public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Customer customer = CommonUtils.toBean(request.getParameterMap(), Customer.class);
customer.setId(CommonUtils.uuid());
customerService.add(customer);
request.setAttribute("msg", "恭喜,成功添加客户");
return "/msg.jsp";
}
// public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// List customers = customerService.findAll();
//
// request.setAttribute("cstmList", customers);
//
// return "/list.jsp";
// }
public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
*1.获取页面传递的pc
* 2.给定pr的值
* 3.使用pc和pr调用service方法,得到pageBean,保存到request域
* 4.转发到list.jsp
*/
/*
* 1.得到pc
* 如果pc参数不存在,说明pc=1
* 如果pc参数存在,需要转换成int类型
*/
int pc = getPc(request);
int pr = 10;//给定pr的值,每页10行纪录
PageBean pb = customerService.findAll(pc, pr);
pb.setUrl(getUrl(request));
request.setAttribute("pb", pb);
return "f:/list.jsp";
}
private int getPc(HttpServletRequest request) {
String value = request.getParameter("pc");
if (value == null || value.trim().isEmpty()) {
return 1;
}
return Integer.parseInt(value);
}
public String preEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
Customer customer = customerService.find(id);
request.setAttribute("customer", customer);
return "/edit.jsp";
}
public String edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Customer customer = CommonUtils.toBean(request.getParameterMap(), Customer.class);
customerService.edit(customer);
request.setAttribute("msg", "恭喜,编辑客户成功");
return "/msg.jsp";
}
public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
customerService.delete(id);
request.setAttribute("msg", "恭喜,删除客户成功");
return "/msg.jsp";
}
// public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
// Customer customer=CommonUtils.toBean(request.getParameterMap(),Customer.class);
//
// List customers=customerService.query(customer);
//
// request.setAttribute("cstmList",customers);
//
// return "/list.jsp";
//
// }
public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Customer customer = CommonUtils.toBean(request.getParameterMap(), Customer.class);
// System.out.println(getUrl(request));
customer = encoding(customer);
int pc = getPc(request);
int pr = 10;
PageBean pb = customerService.query(customer, pc, pr);
pb.setUrl(getUrl(request));
request.setAttribute("pb", pb);
return "/list.jsp";
}
private Customer encoding(Customer customer) throws UnsupportedEncodingException {
String name = customer.getName();
String gender = customer.getGender();
String phone = customer.getPhone();
String email = customer.getEmail();
if (name != null && !name.trim().isEmpty()) {
name = new String(name.getBytes("ISO-8859-1"), "utf-8");
customer.setName(name);
}
if (gender != null && !gender.trim().isEmpty()) {
gender = new String(gender.getBytes("ISO-8859-1"), "utf-8");
customer.setGender(gender);
}
if (phone != null && !phone.trim().isEmpty()) {
phone = new String(phone.getBytes("ISO-8859-1"), "utf-8");
customer.setPhone(phone);
}
if (email != null && !email.trim().isEmpty()) {
email = new String(email.getBytes("ISO-8859-1"), "utf-8");
customer.setEmail(email);
}
return customer;
}
private String getUrl(HttpServletRequest request) {
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String queryString = request.getQueryString();
if (queryString.contains("&pc=")) {
int index = queryString.lastIndexOf("&pc=");
queryString = queryString.substring(0, index);
}
return contextPath + servletPath + "?" + queryString;
}
}
CustomerService
package service;
import dao.CustomerDao;
import domain.Customer;
import domain.PageBean;
public class CustomerService
{
CustomerDao customerDao=new CustomerDao();
public void add(Customer customer)
{
customerDao.add(customer);
}
// public List findAll()
// {
// return customerDao.findAll();
// }
public PageBean findAll(int pc,int pr)
{
return customerDao.findAll(pc,pr);
}
public Customer find(String id)
{
return customerDao.find(id);
}
public void edit(Customer customer)
{
customerDao.edit(customer);
}
public void delete(String id)
{
customerDao.delete(id);
}
public PageBean query(Customer customer,int pc,int pr)
{
return customerDao.query(customer,pc,pr);
}
}
web.xml
CustomerServlet
servlet.CustomerServlet
CustomerServlet
/CustomerServlet
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
frame.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
主页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri ="http://java.sun.com/jsp/jstl/core"%>
Title
编辑客户
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
添加客户
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
客户列表
客户列表
客户姓名
性别
手机
邮箱
描述
操作
${cstm.name}
${cstm.gender}
${cstm.phone}
${cstm.email}
${cstm.description}
编辑
删除
第${pb.pc}页/共${pb.tp}页
首页
上一页
<%--头溢出--%>
<%--尾溢出--%>
<%--循环遍历页码列表--%>
[${i}]
[${i}]
下一页
尾页
msg.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${msg}
query.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
高级搜索
" method="get">
客户名称
客户性别
手机
邮箱
top.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
My JSP 'top.jsp' starting page
客户关系管理系统
添加客户
查询客户
高级搜索
数据库建表语句:
CREATE TABLE `t_customer` (
`id` varchar(50) NOT NULL DEFAULT '',
`name` varchar(50) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`phone` varchar(30) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`description` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;