客户关系管理

package cn.edu.nsu.cstm.servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.edu.nsu.cstm.domain.customer;
import cn.edu.nsu.cstm.service.customerService;
import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;


public class customerServlet extends BaseServlet {
customerService customerService=new customerService();


public String add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);//得到封装好的用户
c.setCid(CommonUtils.uuid());//将用户的id用uuid随机生成的数表似
customerService.add(c);//通过service添加用户
request.setAttribute("msg", c.getCname()+"用户注册成功");//保持信息到msg中
return "f:/msg.jsp";//f代表转发,若是r代表的就是重定向了
}
public String findall(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用service得到用户的对象
//保存到request域中
//转发到list.jsp中
request.setAttribute("cstmList", customerService.findall());
return "f:/list.jsp";
}
public String preEdit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取cid
//使用cid得到service的customer对象
//转发显示到表单中
String cid =request.getParameter("cid");
customer cstm=customerService.load(cid);
request.setAttribute("cstm", cstm);

return "f:/edit.jsp";
}
public String edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//封装表单对象到customer中
//调用service方法完成修改
//保存信息到request域中
//转发到msg.jsp中
customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);
customerService.edit(c);
request.setAttribute("msg", c.getCname()+"用户修改成功");

return "f:/msg.jsp";
}
public String delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//封装表单对象到customer中
//调用service方法完成修改
//保存信息到request域中
//转发到msg.jsp中
customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);
String idString=c.getCname();
customerService.delete(c);
request.setAttribute("msg", idString+"用户删除成功");

return "f:/msg.jsp";
}
public String query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//封装表单数据到customer对象之中,它有四个属性(cname, gender,cellphone,email)
//调用service方法
//保存在request域中
//转发到list.jsp中
customer criteria=CommonUtils.toBean(request.getParameterMap(), customer.class);
List<customer> cstmList=customerService.query(criteria);
request.setAttribute("cstmList", cstmList);
return "f:/list.jsp";
}








}









package cn.edu.nsu.cstm.dao;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.dbutils.QueryRunner;


import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;






import cn.edu.nsu.cstm.domain.customer;
import cn.itcast.jdbc.TxQueryRunner;


public class customerDao {
private QueryRunner qr = new TxQueryRunner();// 依赖此对象对数据库的使用进行操作
// 添加客户


public void add(customer c) {
try {
String sql = "INSERT INTO t_customer VALUES(?,?,?,?,?,?,?)";//插入用户
Object[] params = { c.getCid(), c.getCname(), c.getGender(),
c.getBirthday(), c.getCellphone(), c.getEmail(),
c.getDescription() };//为每一个用户进行赋值操作
qr.update(sql,params);//处理开始
} catch (Exception e) {
throw new RuntimeException();//抛出异常
}
}


public List<customer> findall(){
try {
String sql="select* from  t_customer";
return qr.query(sql, new BeanListHandler<customer>(customer.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}


public customer load(String cid) {
//加载客户
try{
String sql="select *from t_customer where cid=?";
return qr.query(sql, new BeanHandler<customer>(customer.class),cid);
}catch (Exception e) {
throw new RuntimeException();
}
}
//编辑客户
public void edit(customer c) {
try {
String sql="update t_customer set cname=?,gender=?,birthday=?,cellphone=?,email=?,description=? where cid=?";
Object[] parmas={c.getCname(), c.getGender(),
c.getBirthday(), c.getCellphone(), c.getEmail(),
c.getDescription(),c.getCid()};

qr.update(sql, parmas);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}


public void delete(customer c) {
String sql="delete from t_customer where cid=?";
try {
qr.update(sql, c.getCid());
} catch (SQLException e) {
throw new RuntimeException(e);
}

}


public List<customer> quer(customer criteria) {
//给出一个sql的前半句话
StringBuilder sql=new StringBuilder("select *from  t_customer where 1=1");
//判断条件在追加子句
List<Object> param=new ArrayList<Object>();
//创建一个Arraylist用来转载参数
String cname=criteria.getCname();
if(cname!=null&&!cname.trim().isEmpty()){
sql.append(" and cname=?");
param.add(cname);
}
String gender=criteria.getGender();
if(gender!=null&&!gender.trim().isEmpty()){
sql.append(" and gender=?");
param.add(gender);
}
String cellphone=criteria.getCellphone();
if(cellphone!=null&&!cellphone.trim().isEmpty()){
sql.append(" and cellphone=?");
param.add(cellphone);
}
String email=criteria.getEmail();
if(email!=null&&!email.trim().isEmpty()){
sql.append(" and email=?");
param.add(email);
}
//给出参数
try {
return qr.query(sql.toString(), new BeanListHandler<customer>(customer.class),param.toArray());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}


}







package cn.edu.nsu.cstm.service;


import java.util.List;


import cn.edu.nsu.cstm.dao.customerDao;
import cn.edu.nsu.cstm.domain.customer;


public class customerService {
private customerDao customerDao=new customerDao();//依赖此对象得到对象的信息
public void add(customer c){
customerDao.add(c);
}
public List<customer> findall(){
return customerDao.findall();
}
public customer load(String cid) {

return customerDao.load(cid);
}
public void edit(customer c) {
customerDao.edit(c);

}
public void delete(customer c) {
customerDao.delete(c);

}
public List<customer> query(customer criteria) {


return customerDao.quer(criteria);
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值