标签:
前台页面是别人给的。
例子:
package cn.itcast.cus.dao; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import cn.itcast.cus.domain.Customer; import cn.itcast.jdbc.TxQueryRunner; public class CustomerDao { private QueryRunner qr=new TxQueryRunner(); public void add(Customer customer){ String sql="INSERT INTO t_customer VALUES(?,?,?,?,?,?,?)"; Object[] params={customer.getCid(),customer.getCname(),customer.getGender(),customer.getBirthday(),customer.getCellphone(),customer.getEmail(),customer.getDescription()}; try { qr.update(sql, params); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Customer> findAll(){ String sql="SELECT * FROM t_customer"; ResultSetHandler<List<Customer>> rsh=new BeanListHandler<Customer>(Customer.class); ArrayList<Customer> customers=null; try { customers = (ArrayList<Customer>) qr.query(sql, rsh); } catch (SQLException e) { throw new RuntimeException(e); } return customers; } public Customer load(String cid) { String sql="SELECT * FROM t_customer WHERE cid=?"; ResultSetHandler<Customer> rsh=new BeanHandler<Customer>(Customer.class); Object[] params={cid}; Customer customer=null; try { customer = qr.query(sql, rsh,params); } catch (SQLException e) { throw new RuntimeException(e); } return customer; } public void update(Customer c){ String sql="UPDATE t_customer SET cname=?,gender=?,birthday=?,cellphone=?,email=?,description=? WHERE cid=?"; Object[] params={c.getCname(),c.getGender(),c.getBirthday(),c.getCellphone(),c.getEmail(),c.getDescription(),c.getCid()}; try { qr.update(sql, params); } catch (SQLException e) { throw new RuntimeException(e); } } public void delete(String cid) { String sql="DELETE FROM t_customer WHERE cid=?"; Object[] params={cid}; try { qr.update(sql, params); } catch (SQLException e) { throw new RuntimeException(e); } } public List<Customer> query(Customer c) { StringBuilder sb=new StringBuilder("SELECT * FROM t_customer WHERE 1=1"); ArrayList<Object> params=new ArrayList<Object>(); String cname=c.getCname(); if(cname!=null&& !cname.trim().isEmpty()){ sb.append(" and cname like ?"); params.add("%"+cname+"%"); } String gender=c.getGender(); if(gender!=null&&!gender.trim().isEmpty()){ sb.append(" and gender=?"); params.add(gender); } String cellphone=c.getCellphone(); if(cellphone!=null &&!cellphone.trim().isEmpty()){ sb.append(" and cellphone like ?"); params.add("%"+cellphone+"%"); } String email=c.getEmail(); if(email!=null && !email.trim().isEmpty()){ sb.append(" and email like ?"); params.add("%"+email+"%"); } String sql=sb.toString(); System.out.println(sql); List<Customer> cus; try { cus=qr.query(sql, new BeanListHandler<Customer>(Customer.class),params.toArray()); } catch (SQLException e) { throw new RuntimeException(e); } return cus; } } package cn.itcast.cus.dao; import org.junit.Test; import cn.itcast.cus.domain.Customer; import cn.itcast.utils.CommonUtils; public class CustomerTest { @Test public void fun1(){ CustomerDao dao=new CustomerDao(); for(int i=1;i<300;i++){ Customer c=new Customer(); c.setCid(CommonUtils.uuid()); c.setCname("cstnm_"+i); c.setBirthday("2016-08-01"); c.setGender(i%2==0?"男":"女"); c.setCellphone("139"+i); c.setEmail("cstm_"+i+"@163.com"); c.setDescription("我是客户"); dao.add(c); } } } package cn.itcast.cus.domain; import java.io.Serializable; public class Customer implements Serializable{ private static final long serialVersionUID = 1L; /** * 对应数据表 */ private String cid;//主键 private String cname;//姓名 private String gender;//性别 private String birthday;//生日 private String cellphone;//电话 private String email;//邮箱 private String description;//描述 public Customer() { super(); // TODO Auto-generated constructor stub } public Customer(String cid, String cname, String gender, String birthday, String cellphone, String email, String description) { super(); this.cid = cid; this.cname = cname; this.gender = gender; this.birthday = birthday; this.cellphone = cellphone; this.email = email; this.description = description; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getCellphone() { return cellphone; } public void setCellphone(String cellphone) { this.cellphone = cellphone; } 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; } @Override public String toString() { return "Customer [cid=" + cid + ", cname=" + cname + ", gender=" + gender + ", birthday=" + birthday + ", cellphone=" + cellphone + ", email=" + email + ", description=" + description + "]"; } } package cn.itcast.cus.service; import java.util.List; import cn.itcast.cus.dao.CustomerDao; import cn.itcast.cus.domain.Customer; public class CustomerService { private CustomerDao customerDao=new CustomerDao(); public void addCustomer(Customer customer){ customerDao.add(customer); } public List<Customer> findAll(){ return customerDao.findAll(); } public Customer load(String cid){ return customerDao.load(cid); } public void update(Customer c){ customerDao.update(c); } public void delete(String cid) { customerDao.delete(cid); } public List<Customer> query(Customer c) { return customerDao.query(c); } } package cn.itcast.cus.web.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.cus.domain.Customer; import cn.itcast.cus.service.CustomerService; import cn.itcast.servlet.BaseServlet; import cn.itcast.utils.CommonUtils; public class CustomerServlet extends BaseServlet { private static final long serialVersionUID = 1L; CustomerService customerService=new CustomerService(); public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、补全表单数据到Customer对象 * 2、补全Cid,使用uuid * 3、使用service方法完成添加工作 * 4.向request域中保存成功信息 * 5、转发到msg.jsp */ Customer c=CommonUtils.toBean(request.getParameterMap(), Customer.class); c.setCid(CommonUtils.uuid()); customerService.addCustomer(c); request.setAttribute("msg", "恭喜!注册成功"); return "f:/msg.jsp"; } public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、直接调用service方法,得到查询结果 * 2、查询结果保存到request域中 * 3、转发到list.jsp */ List<Customer> customers= customerService.findAll(); request.setAttribute("customers", customers); return "f:/list.jsp"; } public String preEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、直接调用service方法,得到查询结果 * 2、查询结果保存到request域中 * 3、转发到EDIT.jsp */ String cid=request.getParameter("cid"); Customer c=customerService.load(cid); request.setAttribute("customer", c); return "f:/edit.jsp"; } public String edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、封装表单数据到Customer对象中 * 2、调用service完成编辑 * 3、保存成功信息到request域 * 4、转发到msg.jsp显示成功信息 */ Customer c=CommonUtils.toBean(request.getParameterMap(), Customer.class); customerService.update(c); request.setAttribute("msg", "恭喜,修改成功!"); return "f:/msg.jsp"; } public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、封装表单数据到Customer对象中 * 2、调用service完成编辑 * 3、保存成功信息到request域 * 4、转发到msg.jsp显示成功信息 */ String cid=request.getParameter("cid"); customerService.delete(cid); request.setAttribute("msg", "恭喜,删除成功!"); return "f:/msg.jsp"; } public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 1、封装表单数据到Customer对象中 * 2、调用service完成编辑 * 3、保存查询到request域 * 4、转发到list.jsp */ Customer criteria=CommonUtils.toBean(request.getParameterMap(), Customer.class); List<Customer> customers=customerService.query(criteria); request.setAttribute("customers", customers); return "f:/list.jsp"; } } <?xml version="1.0" encoding="UTF-8" ?> <c3p0-config> <!-- 默认连接配置 --> <default-config> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/customers</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">guodaxia</property> <property name="password">961012gz</property> <!-- 池参数配置 --> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <named-config name="oracle-config"> <!-- 连接四大参数配置 --> <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:db</property> <property name="driverClass">oracle.jdbc.driver.OracleDriver</property> <property name="user">scott</property> <property name="password">961012gz</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>添加客户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="<c:url value=‘/jquery/jquery.datepick.css‘/>"> <script type="text/javascript" src="<c:url value=‘/jquery/jquery-1.5.1.js‘/>"></script> <script type="text/javascript" src="<c:url value=‘/jquery/jquery.datepick.js‘/>"></script> <script type="text/javascript" src="<c:url value=‘/jquery/jquery.datepick-zh-CN.js‘/>"></script> <script type="text/javascript"> $(function() { $("#birthday").datepick({dateFormat:"yy-mm-dd"}); }); function add() { $(".error").text(""); var bool = true; if(!$(":text[name=cname]").val()) { $("#cnameError").text("客户名称不能为空"); bool = false; } if(!$("#male").attr("checked") && !$("#female").attr("checked")) { $("#genderError").text("客户性别不能为空"); bool = false; } if(!$(":text[name=cellphone]").val()) { $("#cellphoneError").text("手机不能为空"); bool = false; } if(!$(":text[name=email]").val()) { $("#emailError").text("email不能为空"); bool = false; } if(bool) { $("form").submit(); } } </script> <style type="text/css"> .error {color:red;} </style> </head> <body> <h3 align="center">添加客户</h3> <form action="<c:url value=‘/CustomerServlet‘/>" method="post"> <!-- 向servlet传递一个名为method的参数,其值表示要调用servlet的哪一个方法 --> <input type="hidden" name="method" value="add"/> <table border="0" align="center" width="40%" style="margin-left: 100px;"> <tr> <td width="100px">客户名称</td> <td width="40%"> <input type="text" name="cname"/> </td> <td align="left"> <label id="cnameError" class="error"> </label> </td> </tr> <tr> <td>客户性别</td> <td> <input type="radio" name="gender" value="男" id="male"/> <label for="male">男</label> <input type="radio" name="gender" value="女" id="female"/> <label for="female">女</label> </td> <td> <label id="genderError" class="error"> </label> </td> </tr> <tr> <td>客户生日</td> <td> <input type="text" name="birthday" id="birthday" readonly="readonly"/> </td> <td> <label id="birthdayError" class="error"> </label> </td> </tr> <tr> <td>手机</td> <td> <input type="text" name="cellphone"/> </td> <td> <label id="cellphoneError" class="error"> </label> </td> </tr> <tr> <td>邮箱</td> <td> <input type="text" name="email"/> </td> <td> <label id="emailError" class="error"> </label> </td> </tr> <tr> <td>描述</td> <td> <textarea rows="5" cols="30" name="description"></textarea> </td> <td> <label id="descriptionError" class="error"> </label> </td> </tr> <tr> <td> </td> <td> <input type="button" value="添加客户" onclick="add()"/> <input type="reset" value="重置"/> </td> <td> </td> </tr> </table> </form> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>编辑客户</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="<c:url value=‘/jquery/jquery.datepick.css‘/>"> <script type="text/javascript" src="<c:url value=‘/jquery/jquery-1.5.1.js‘/>"></script> <script type="text/javascript" src="<c:url value=‘/jquery/jquery.datepick.js‘/>"></script> <script type="text/javascript" src="<c:url value=‘/jquery/jquery.datepick-zh-CN.js‘/>"></script> <script type="text/javascript"> $(function() { $("#birthday").datepick({dateFormat:"yy-mm-dd"}); }); function add() { $(".error").text(""); var bool = true; if(!$(":text[name=cname]").val()) { $("#cnameError").text("客户名称不能为空"); bool = false; } if(!$("#male").attr("checked") && !$("#female").attr("checked")) { $("#genderError").text("客户性别不能为空"); bool = false; } if(!$(":text[name=cellphone]").val()) { $("#cellphoneError").text("手机不能为空"); bool = false; } if(!$(":text[name=email]").val()) { $("#emailError").text("email不能为空"); bool = false; } if(bool) { $("form").submit(); } } </script> <style type="text/css"> .error {color:red;} </style> </head> <body> <h3 align="center">编辑客户</h3> <form action="<c:url value=‘CustomerServlet‘/>" method="post"> <input type="hidden" name="method" value="edit"> <input type="hidden" name="cid" value="${customer.cid }"> <table border="0" align="center" width="40%" style="margin-left: 100px;"> <tr> <td width="100px">客户名称</td> <td width="40%"> <input type="text" name="cname" value="${customer.cname }"/> </td> <td align="left"> <label id="cnameError" class="error"> </label> </td> </tr> <tr> <td>客户性别</td> <td> <input type="radio" name="gender" value="男" id="male" <c:if test="${customer.gender eq ‘男‘ }">checked=‘checked‘</c:if> /> <label for="male">男</label> <input type="radio" name="gender" value="女" id="female" <c:if test="${customer.gender eq ‘女‘ }">checked=‘checked‘</c:if>/> <label for="female">女</label> </td> <td> <label id="genderError" class="error"> </label> </td> </tr> <tr> <td>客户生日</td> <td> <input type="text" name="birthday" id="birthday" readonly="readonly" value="${customer.birthday }"/> </td> <td> <label id="birthdayError" class="error"> </label> </td> </tr> <tr> <td>手机</td> <td> <input type="text" name="cellphone" value="${customer.cellphone }"/> </td> <td> <label id="cellphoneError" class="error"> </label> </td> </tr> <tr> <td>邮箱</td> <td> <input type="text" name="email" value="${customer.email }"/> </td> <td> <label id="emailError" class="error"> </label> </td> </tr> <tr> <td>描述</td> <td> <textarea rows="5" cols="30" name="description">${customer.description }</textarea> </td> <td> <label id="descriptionError" class="error"> </label> </td> </tr> <tr> <td> </td> <td> <input type="button" value="编辑客户" onclick="add()"/> <input type="reset" value="重置"/> </td> <td> </td> </tr> </table> </form> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>主页</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <frameset rows="20%,*"> <frame src="<c:url value=‘/top.jsp‘/>" name="top"/> <frame src="<c:url value=‘/welcome.jsp‘/>" name="main"/> </frameset> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <jsp:forward page="/frame.jsp" /> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>客户列表</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3 align="center">客户列表</h3> <table border="1" width="70%" align="center"> <tr> <th>客户姓名</th> <th>性别</th> <th>生日</th> <th>手机</th> <th>邮箱</th> <th>描述</th> <th>操作</th> </tr> <c:forEach var="c" items="${requestScope.customers }"> <tr> <td>${c.cname }</td> <td>${c.gender }</td> <td>${c.birthday }</td> <td>${c.cellphone }</td> <td>${c.email }</td> <td>${c.description }</td> <td> <a href="<c:url value=‘/CustomerServlet?cid=${c.cid }&method=preEdit‘/>">编辑</a> <a href="<c:url value=‘/CustomerServlet?cid=${c.cid }&method=delete‘/>">删除</a> </td> </tr> </c:forEach> </table> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘msg.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1 style="color:green;" align="center">${msg }</h1> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>高级搜索</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3 align="center">高级搜索</h3> <form action="<c:url value=‘/CustomerServlet‘/>" method="post"> <input type="hidden" name="method" value="query"> <table border="0" align="center" width="40%" style="margin-left: 100px;"> <tr> <td width="100px">客户名称</td> <td width="40%"> <input type="text" name="cname"/> </td> </tr> <tr> <td>客户性别</td> <td> <select name="gender"> <option value="">==请选择性别==</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>手机</td> <td> <input type="text" name="cellphone"/> </td> <td> <label id="cellphoneError" class="error"> </label> </td> </tr> <tr> <td>邮箱</td> <td> <input type="text" name="email"/> </td> <td> <label id="emailError" class="error"> </label> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="搜索"/> <input type="reset" value="重置"/> </td> <td> </td> </tr> </table> </form> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base target="main"> <title>My JSP ‘top.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body style="text-align: center;"> <h1>客户关系管理系统</h1> <a href="<c:url value=‘/add.jsp‘/>">添加客户</a> | <a href="<c:url value=‘/CustomerServlet?method=findAll‘/>">查询客户</a> | <a href="<c:url value=‘/query.jsp‘/>">高级搜索</a> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘welcome.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1 align="center">欢迎登录本系统</h1> </body> </html>
js太恶心了就不传了,别人写的js总是有点看起来多。
感觉一些结构很清晰,很适合代码规范练习
标签:
原文地址:http://www.cnblogs.com/aigeileshei/p/5727170.html