作业终于做完了,好开心。。。。。。虽然这一周经历不是那么顺利,但是觉得还是收获了不少,有过想哭的冲动,代码不会写,事情办不好,各种发愁。空间里发小发了带父母出去游玩的照片,瞬间能量值不知道是被击退的多还是回返的多。不过今天终于补满能量值。下面我来分享一下我的运行结果。

下面我把我管理后台的代码粘贴一下

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3.  
  4.  
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  6. <html>
  7. <head>
  8. <title>top.jsp</title>
  9. <style type="text/css">
  10. body {
  11. text-align: center;
  12. font-size: 38px;
  13. }
  14. * {
  15. font-family: 华文楷体;
  16. font-size: 18px;
  17. color: #F0F ";
  18. }
  19. h1 {
  20. text-align: center;
  21. font-size: 38px;
  22. }
  23. </style>
  24. </head>
  25.  
  26. <body>
  27. <h1 style="font-size: 38px;">
  28. 欢迎登录网上购物后台
  29. </h1>
  30.  
  31. <form action="<c:url value=\'/AdminServlet\'/>" method="post">
  32. <input type="hidden" name="method" value="1">
  33. 用户名:
  34. <input type="text" name="username" />
  35. <br />
  36. &nbsp; 码:
  37. <input type="password" name="password" />
  38. <br />
  39. <input type="submit" value="登录">
  40. </form>
  41. </body>
  42. </html>

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5. <head>
  6. <title>list.jsp</title>
  7. <style type="text/css">
  8. body {
  9. text-align: center;
  10. font-size: 38px;
  11. }
  12. * {
  13. font-family: 华文楷体;
  14. font-size: 18px;
  15. color: #F0F ";
  16. }
  17. img{
  18. width: 50px;
  19. height: 30px;
  20.  
  21.  
  22. }
  23. h1 {
  24. text-align: center;
  25. font-size: 38px;
  26. }
  27. a:link {
  28. color: #999;
  29. text-decoration: none;
  30. }
  31. a:VISITED {
  32. text-decoration: none;
  33. color: #c60;
  34. }
  35. a:HOVER {
  36. text-decoration: none;
  37. color: #00F;
  38. }
  39. </style>
  40.  
  41. </head>
  42.  
  43. <body>
  44. <h1 style="font-size: 38px;">
  45. 欢迎登录到网上购物后台系统
  46. </h1>
  47. <a style="font-size: 26px;"
  48. href="<c:url value=\'/AdminServlet?method=2\'/>">查看商品</a>
  49. <a style="font-size: 26px;" href="<c:url value=\'/admin/add.jsp\'/>">添加商品</a>
  50. <a style="font-size: 26px;"
  51. href="<c:url value=\'/AdminServlet?method=7\'/>">查看订单</a>
  52. <a style="font-size: 26px;" href="<c:url value=\'/admin/login.jsp\'/>">退出系统</a>
  53.  
  54. <table border="1" align="center" width="60%">
  55. <tr>
  56. <th>
  57. 商品名
  58. </th>
  59. <th>
  60. 价格
  61. </th>
  62. <th>
  63. 类型
  64. </th>
  65. <th>
  66. 数量
  67. </th>
  68. <th>
  69. 图片
  70. </th>
  71. <th>
  72. 描述
  73. </th>
  74. <th>
  75. 操作
  76. </th>
  77.  
  78. </tr>
  79. <c:forEach items="${pb.datas }" var="c">
  80. <tr>
  81. <td>
  82. ${c.name }
  83. </td>
  84. <td>
  85. ${c.price }
  86. </td>
  87. <td>
  88. ${c.category }
  89. </td>
  90. <td>
  91. ${c.pnum}
  92. </td>
  93. <td>
  94. <a href="<c:url value=\'/AdminServlet?method=9&id=${c.id }\'/>"><img src="${c.imgurl }"/> </a>
  95. </td>
  96. <td>
  97. ${c.description }
  98. </td>
  99.  
  100. <td>
  101. <a href="<c:url value=\'/AdminServlet?method=4&id=${c.id }\'/>">编辑</a>
  102. <a href="<c:url value=\'/OrderServlet?method=4&id=${c.id }\'/>">删除</a>
  103. </td>
  104. </tr>
  105. </c:forEach>
  106. </table>
  107. <br>
  108. 第${pb.pageCode }页/共${pb.totalPage }页
  109. <a href="<c:url value=\'/AdminServlet?method=2\'/>">首页</a>
  110. <c:if test="${pb.pageCode > 1}">
  111. <a
  112. href="<c:url value=\'/AdminServlet?method=2&pageCode=${pb.pageCode-1}\'/>">上一页</a>
  113. </c:if>
  114. <%--页码列表 --%>
  115. <c:set var="begin" value="1" />
  116. <c:set var="end" value="10" />
  117.  
  118. <!-- 定位begin和end -->
  119. <c:choose>
  120. <c:when test="${pb.totalPage <= 10}">
  121. <c:set var="begin" value="1" />
  122. <c:set var="end" value="${pb.totalPage}" />
  123. </c:when>
  124. <c:otherwise>
  125. <c:choose>
  126. <c:when test="${pb.pageCode-4 <= 1 }">
  127. <c:set var="begin" value="1" />
  128. <c:set var="end" value="10" />
  129. </c:when>
  130. <c:when test="${pb.pageCode+5 > pb.totalPage}">
  131. <c:set var="begin" value="${pb.totalPage-9}" />
  132. <c:set var="end" value="${pb.totalPage}" />
  133. </c:when>
  134. <c:otherwise>
  135. <c:set var="begin" value="${pb.pageCode-4}" />
  136. <c:set var="end" value="${pb.pageCode+5}" />
  137. </c:otherwise>
  138. </c:choose>
  139. </c:otherwise>
  140. </c:choose>
  141. <c:forEach begin="${begin}" end="${end}" var="i">
  142. <c:choose>
  143. <c:when test="${pb.pageCode == i}">${i}</c:when>
  144. <c:otherwise>
  145. <a href="<c:url value=\'/AdminServlet?method=2&pageCode=${i}\'/>">${i
  146. }</a>
  147. </c:otherwise>
  148. </c:choose>
  149. </c:forEach>
  150.  
  151. <c:if test="${pb.pageCode < pb.totalPage}">
  152. <a
  153. href="<c:url value=\'/AdminServlet?method=2&pageCode=${pb.pageCode+1}\'/>">下一页</a>
  154. </c:if>
  155. <a
  156. href="<c:url value=\'/AdminServlet?method=2&pageCode=${pb.totalPage }\'/>">尾页</a>
  157.  
  158. </body>
  159. </html>

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3.  
  4.  
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  6. <html>
  7. <head>
  8. <title>add.jsp</title>
  9. <style type="text/css">
  10. body {
  11. text-align: center;
  12. font-size: 38px;
  13. }
  14. * {
  15. font-family: 华文楷体;
  16. font-size: 18px;
  17. color: #F0F ";
  18. }
  19. h1 {
  20. text-align: center;
  21. font-size: 38px;
  22. }
  23. </style>
  24.  
  25. </head>
  26.  
  27. <body>
  28.  
  29. <h1 style="font-size: 38px;">
  30. 添加商品
  31. </h1>
  32.  
  33. <form action="<c:url value=\'/AdminServlet\'/>" method="post">
  34. <input type="hidden" name="method" value="3">
  35. <table border="1" width="50%" align="center">
  36. <tr>
  37. <td>
  38. 商品名
  39. </td>
  40. <td>
  41. <input type="text" name="name" id="name" onfocus="nameFocus()"
  42. onblur="nameBlur()" />
  43. <div id="nameId"></div>
  44. </td>
  45. </tr>
  46. <tr>
  47. <td>
  48. 价格
  49. </td>
  50. <td>
  51. <input type="text" name="price" id="tel" onfocus="telFocus()"
  52. onblur="telBlur()" />
  53. <div id="telId"></div>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td>
  58. 类型
  59. </td>
  60. <td>
  61. <input type="text" name="category" id="address" />
  62. </td>
  63. </tr>
  64. <tr>
  65. <td>
  66. 数量
  67. </td>
  68. <td>
  69. <input type="text" name="pnum" />
  70. </td>
  71. </tr>
  72. <tr>
  73. <td>
  74. 描述
  75. </td>
  76. <td>
  77. <input type="text" name="description" />
  78. </td>
  79. </tr>
  80.  
  81.  
  82. </table>
  83. <input style="margin-right: 200px;" type="submit" value="添加" />
  84.  
  85. </form>
  86. </body>
  87. </html>

 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3.  
  4.  
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  6. <html>
  7. <head>
  8. <title>edit.jsp</title>
  9. </head>
  10.  
  11. <body style="text-align: center;">
  12. <h3>
  13. 修改商品信息
  14. </h3>
  15. <form action="<c:url value=\'/AdminServlet\'/>" method="post">
  16. <input type="hidden" name="method" value="5">
  17. <input type="hidden" name="id" value="${product.id }">
  18. <table border="1" width="50%" align="center">
  19. <tr>
  20. <td>
  21. 商品名
  22. </td>
  23. <td>
  24. <input type="text" name="name" value="${product.name }" />
  25. </td>
  26. </tr>
  27. <tr>
  28. <td>
  29. 价格
  30. </td>
  31. <td>
  32. <input type="text" name="price" value="${product.price }" />
  33. </td>
  34. </tr>
  35. <tr>
  36. <td>
  37. 类型
  38. </td>
  39. <td>
  40. <input type="text" id="birthday" name="category"
  41. value="${product.category }" />
  42. </td>
  43. </tr>
  44. <tr>
  45. <td>
  46. 数量
  47. </td>
  48. <td>
  49. <input type="text" name="pnum" value="${product.pnum }" />
  50. </td>
  51. </tr>
  52.  
  53. <tr>
  54. <td>
  55. 描述
  56. </td>
  57. <td>
  58. <input type="text" name="description"
  59. value="${product.description }" />
  60. </td>
  61. </tr>
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5. <head>
  6. <title>list.jsp</title>
  7. </head>
  8.  
  9. <body>
  10. <center>
  11. <h1>
  12. 查看所有订单
  13. </h1>
  14. <table border="2" width="80%">
  15. <tr align="left">
  16. <th>
  17. 序号
  18. </th>
  19. <th>
  20. 订单号
  21. </th>
  22. <th>
  23. 订单金额
  24. </th>
  25. <th>
  26. 时间
  27. </th>
  28. <th>
  29. 状态
  30. </th>
  31. <th>
  32. 买家详情
  33. </th>
  34. <th>
  35. 物流信息
  36. </th>
  37. <th>
  38. 交易状态
  39. </th>
  40. <th>
  41. 操作
  42. </th>
  43. </tr>
  44. <c:forEach items="${allorderList}" var="order" varStatus="status">
  45. <tr>
  46. <td>
  47. ${status.count}
  48. </td>
  49. <td>
  50. ${order.id }
  51. </td>
  52. <td>
  53. ${order.totalMoney }
  54. </td>
  55. <td>
  56. ${order.ordertime}
  57. </td>
  58. <td>
  59. <c:if test="${order.paysate == 0 }">
  60. 未支付
  61. </c:if>
  62. <c:if test="${order.paysate == 1 }">
  63. 已支付
  64. </c:if>
  65. </td>
  66. <td>
  67. ${order.receiverName }
  68. </td>
  69. <td>
  70. <c:if test="${order.paysate == 0 }">
  71. 未发货
  72. </c:if>
  73. <c:if test="${order.paysate == 1 }">
  74. 已发货
  75. </c:if>
  76. </td>
  77. <td>
  78. 交易中
  79. </td>
  80. <td>
  81. <a href="<c:url value=\'/OrderServlet?method=4&id=${order.id }\'/>">删除</a>
  82. </td>
  83. </tr>
  84. <c:set value="${total + entry.key.price*entry.value}" var="total" />
  85. </c:forEach>
  86. </table>
  87. </center>
  88. </body>
  89. </html>

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  4. <html>
  5. <head>
  6. <title>updateorder.jsp</title>
  7. </head>
  8.  
  9. <body style="text-align: center;">
  10. <h3>
  11. 修改订单信息
  12. </h3>
  13. <form action="<c:url value=\'/AdminServlet\'/>" method="post">
  14. <input type="hidden" name="method" value="5">
  15. <input type="hidden" name="id" value="${product.id }">
  16. <table border="1" width="50%" align="center">
  17. <tr>
  18. <td>
  19. 订单号
  20. </td>
  21. <td>
  22. ${order.id }
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>
  27. 买家姓名
  28. </td>
  29. <td>
  30. <input type="text" name="receiverName"
  31. value="${order.receiverName }" />
  32. </td>
  33. </tr>
  34. <tr>
  35. <td>
  36. 收货地址
  37. </td>
  38. <td>
  39. <input type="text" id="birthday" name="receiverAddress"
  40. value="${order.receiverAddress }" />
  41. </td>
  42. </tr>
  43. <tr>
  44. <td>
  45. 收获电话
  46. </td>
  47. <td>
  48. <input type="text" name="receiverPhone"
  49. value="${order.receiverPhone }" />
  50. </td>
  51. </tr>
  52. <tr>
  53. <td>
  54. 物流信息
  55. </td>
  56. <td>
  57. 在路上
  58. </td>
  59. </tr>
  60. <tr>
  61. <td>
  62. 交易状态
  63. </td>
  64. <td>
  65. 交易中
  66. </td>
  67. </tr>
  68. <tr>
  69. <td colspan="2" align="center">
  70. <input type="submit" value="更改" />
  71. </td>
  72. </tr>
  73. </table>
  74. </form>
  75. </body>
  76. </html>

 

  1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5. "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>upload.jsp</title>
  10. </head>
  11. <body>
  12. <form method="post" action="/shopping/AdminServlet?method=8&id=${product.id }"
  13. enctype="multipart/form-data">
  14. 选择一个文件:
  15. <input type="file" name="imgurl"/>
  16. <br />
  17. <br />
  18. <input type="submit" value="上传" />
  19. </form>
  20. </body>
  21. </html>

 

  1.  

 

  1. <tr>
  2. <td colspan="2" align="center">
  3. <input type="submit" value="更改" />
  4. </td>
  5. </tr>
  6. </table>
  7. </form>
  8. </body>
  9. </html>
  1. package cn.edu.aynu.rjxy.servlet;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.List;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import javax.servlet.http.HttpSession;
  14. import org.apache.commons.fileupload.FileItem;
  15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  16. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  17. import cn.edu.aynu.rjxy.bean.Admin;
  18. import cn.edu.aynu.rjxy.bean.Order;
  19. import cn.edu.aynu.rjxy.bean.Page;
  20. import cn.edu.aynu.rjxy.bean.Product;
  21. import cn.edu.aynu.rjxy.service.AdminService;
  22. import cn.edu.aynu.rjxy.service.OrderService;
  23. import cn.edu.aynu.rjxy.service.ProductService;
  24. import cn.edu.aynu.rjxy.utils.CommonsUtils;
  25. public class AdminServlet extends HttpServlet {
  26. private AdminService aService = new AdminService();
  27. private ProductService ps = new ProductService();
  28. private OrderService os = new OrderService();
  29. private static final long serialVersionUID = 1L;
  30. // 上传文件存储目录
  31. private static final String UPLOAD_DIRECTORY = "img";
  32. // 上传配置
  33. private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
  34. private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
  35. private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
  36.  
  37. public void doGet(HttpServletRequest request, HttpServletResponse response)
  38. throws ServletException, IOException {
  39. this.doPost(request, response);
  40. }
  41. public void doPost(HttpServletRequest request, HttpServletResponse response)
  42. throws ServletException, IOException {
  43. // 处理中文乱码
  44. request.setCharacterEncoding("UTF-8");
  45. response.setContentType("text/html;charset=UTF-8");
  46. // 获取隐藏字段method的值,并把它转换为int型
  47. int method = Integer.parseInt(request.getParameter("method"));
  48. switch (method) {
  49. case 1:
  50. this.login(request, response);
  51. break;
  52. case 2:
  53. this.query(request, response);
  54. break;
  55. case 3:
  56. this.add(request, response);
  57. break;
  58. case 4:
  59. this.loadForUpdate(request, response);
  60. break;
  61. case 5:
  62. this.edit(request, response);
  63. break;
  64. case 6:
  65. this.delete(request, response);
  66. break;
  67. case 7:
  68. this.order(request, response);
  69. break;
  70. case 8:
  71. this.uploadpicture(request, response);
  72. break;
  73. case 9:
  74. this.uploadForpicture(request, response);
  75. break;
  76. }
  77. }
  78. // 管理员登录
  79. public void login(HttpServletRequest request, HttpServletResponse response)
  80. throws ServletException, IOException {
  81. // 从请求中解析用户名和密码
  82. String username = request.getParameter("username");
  83. String password = request.getParameter("password");
  84. // 执行加载业务
  85. Admin a = aService.login(username, password);
  86. if (a != null) {
  87. HttpSession session = request.getSession();
  88. session.setAttribute("userId", a.getId());
  89. response.getWriter().print("登录成功" + a.getId());
  90. // 通过请求转发将pb带到list.jsp上去
  91. request.getRequestDispatcher("/admin/list.jsp").forward(request,
  92. response);
  93. } else {
  94. response.getWriter().print("登录失败,即将跳转到登录页面,请重新登录......");
  95. response.setHeader("Refresh", "3;url=/shopping/admin/login.jsp");
  96. }
  97. }
  98. // 管理员查看商品
  99. public void query(HttpServletRequest request, HttpServletResponse response)
  100. throws ServletException, IOException {
  101. // 当前页有一个默认值为1
  102. int pageCode = 1;
  103. String s = request.getParameter("pageCode");
  104. // 如果请求中存在pageCode参数
  105. if (s != null && !s.trim().isEmpty()) {
  106. pageCode = Integer.parseInt(s);
  107. }
  108. // 查询当前页的记录
  109. Page pb = ps.queryByPage(pageCode);
  110. // 将pb保存在request域中
  111. request.setAttribute("pb", pb);
  112. // 通过请求转发将pb带到list.jsp上去
  113. request.getRequestDispatcher("/admin/list.jsp").forward(request,
  114. response);
  115. }
  116. // 管理员添加商品
  117. public void add(HttpServletRequest request, HttpServletResponse response)
  118. throws ServletException, IOException {
  119. // 使用CommonUtils工具类将请求数据封装到bean中
  120. Product p = CommonsUtils.toBean(request.getParameterMap(),
  121. Product.class);
  122. // 使用UUID作为id属性的值
  123. p.setId(CommonsUtils.uuid());
  124. // p.setImgurl(uploadPath);
  125. // 执行添加
  126. ps.add(p);
  127. // 输出添加成功提示
  128. response.getWriter().print("添加成功");
  129. }
  130. // 加载商品为修改做准备
  131. public void loadForUpdate(HttpServletRequest request,
  132. HttpServletResponse response) throws ServletException, IOException {
  133. // 从请求中获取客户的id
  134. String id = request.getParameter("id");
  135. // 执行加载业务
  136. Product p = ps.findById(id);
  137. // 将Customer对象c保存在request域中
  138. request.setAttribute("product", p);
  139. // t=通过请求转发将customer对象显示在list.jsp页面上
  140. request.getRequestDispatcher("/admin/edit.jsp").forward(request,
  141. response);
  142. }
  143. // 管理员修改商品信息
  144. public void edit(HttpServletRequest request, HttpServletResponse response)
  145. throws ServletException, IOException {
  146. // 使用CommonUtils工具类将请求数据封装到bean中
  147. Product p = CommonsUtils.toBean(request.getParameterMap(),
  148. Product.class);
  149. // 执行更改业务
  150. ps.edit(p);
  151. // 给客户端发送更改成功提示
  152. response.getWriter().print("更改成功");
  153. }
  154. // 管理员删除商品
  155. public void delete(HttpServletRequest request, HttpServletResponse response)
  156. throws ServletException, IOException {
  157. // 从请求中获取id
  158. String id = request.getParameter("id");
  159. // 执行删除
  160. ps.delete(id);
  161. // 给客户端发送删除成功提示
  162. response.getWriter().print("删除成功");
  163. }
  164. // 管理员查看所有订单
  165. public void order(HttpServletRequest request, HttpServletResponse response)
  166. throws ServletException, IOException {
  167. List<Order> list = os.findAllOrder();
  168. request.setAttribute("allorderList", list);
  169. request.getRequestDispatcher("/admin/orderproduct.jsp").forward(
  170. request, response);
  171. }
  172. // 为上传照片做准备
  173. public void uploadForpicture(HttpServletRequest request,
  174. HttpServletResponse response) throws ServletException, IOException {
  175. String id = request.getParameter("id");
  176. Product p = ps.findById(id);
  177. System.out.println(id + "id");
  178. ps.upload(p, id);
  179. // 将Customer对象c保存在request域中
  180. request.setAttribute("product", p);
  181. // t=通过请求转发将customer对象显示在list.jsp页面上
  182. request.getRequestDispatcher("/admin/upload.jsp").forward(request,
  183. response);
  184. }
  185. // 管理员上传照片
  186. public void uploadpicture(HttpServletRequest request,
  187. HttpServletResponse response) throws ServletException, IOException {
  188. // 检测是否为多媒体上传
  189. if (!ServletFileUpload.isMultipartContent(request)) {
  190. // 如果不是则停止
  191. PrintWriter writer = response.getWriter();
  192. writer.println("Error: 表单必须包含 enctype=multipart/form-data");
  193. writer.flush();
  194. return;
  195. }
  196. // 配置上传参数
  197. DiskFileItemFactory factory = new DiskFileItemFactory();
  198. // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
  199. factory.setSizeThreshold(MEMORY_THRESHOLD);
  200. // 设置临时存储目录
  201. factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
  202. ServletFileUpload upload = new ServletFileUpload(factory);
  203. // 设置最大文件上传值
  204. upload.setFileSizeMax(MAX_FILE_SIZE);
  205. // 设置最大请求值 (包含文件和表单数据)
  206. upload.setSizeMax(MAX_REQUEST_SIZE);
  207. // 构造临时路径来存储上传的文件
  208. // 这个路径相对当前应用的目录
  209. String uploadPath = getServletContext().getRealPath("")
  210. + File.separator + UPLOAD_DIRECTORY;
  211. // 如果目录不存在则创建
  212. File uploadDir = new File(uploadPath);
  213. if (!uploadDir.exists()) {
  214. uploadDir.mkdir();
  215. }
  216. try {
  217. // 解析请求的内容提取文件数据
  218. @SuppressWarnings("unchecked")
  219. List<FileItem> formItems = upload.parseRequest(request);
  220. if (formItems != null && formItems.size() > 0) {
  221. // 迭代表单数据
  222. for (FileItem item : formItems) {
  223. // 处理不在表单中的字段
  224. if (!item.isFormField()) {
  225. Date date = new Date();
  226. DateFormat format = new SimpleDateFormat(
  227. "yyyy-MM-dd-HH-mm-ss");
  228. String time = format.format(date);
  229. String filePath = uploadPath + File.separator + time
  230. + ".jpg";
  231. File storeFile = new File(filePath);
  232. // 使用CommonUtils工具类将请求数据封装到bean中
  233. Product p = CommonsUtils.toBean(request
  234. .getParameterMap(), Product.class);
  235. String url = "/" + "shopping" + "/" + UPLOAD_DIRECTORY
  236. + "/" + time + ".jpg";
  237. p.setImgurl(url);
  238. // 执行更改业务
  239. ps.upload(p, p.getId());
  240. // 在控制台输出文件的上传路径
  241. System.out.println(filePath);
  242. System.out.println("/" + "shopping" + "/"
  243. + UPLOAD_DIRECTORY + "/" + time + ".jpg");
  244. // 保存文件到硬盘
  245. item.write(storeFile);
  246. request.setAttribute("message", "文件上传成功!");
  247. }
  248. }
  249. }
  250. } catch (Exception ex) {
  251. request.setAttribute("message", "错误信息: " + ex.getMessage());
  252. }
  253. response.getWriter().print("上传成功,三秒后跳转到功能界面......");
  254. response.setHeader("Refresh", "3;url=/shopping/admin/list.jsp");
  255. }
  256. }

 

  1. package cn.edu.aynu.rjxy.servlet;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import cn.edu.aynu.rjxy.bean.Order;
  12. import cn.edu.aynu.rjxy.bean.OrderItem;
  13. import cn.edu.aynu.rjxy.bean.Product;
  14. import cn.edu.aynu.rjxy.bean.User;
  15. import cn.edu.aynu.rjxy.service.OrderService;
  16. import cn.edu.aynu.rjxy.utils.CommonsUtils;
  17. public class OrderServlet extends HttpServlet {
  18. private OrderService orderService = new OrderService();
  19. public void doGet(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException, IOException {
  21. this.doPost(request, response);
  22. }
  23. public void doPost(HttpServletRequest request, HttpServletResponse response)
  24. throws ServletException, IOException {
  25. // 处理中文乱码
  26. request.setCharacterEncoding("UTF-8");
  27. response.setContentType("text/html;charset=UTF-8");
  28. // 获取隐藏字段method的值,并把它转换为int型
  29. int method = Integer.parseInt(request.getParameter("method"));
  30. switch (method) {
  31. case 1:
  32. this.addOrder(request, response);
  33. break;
  34. case 2:
  35. this.findMyOrder(request, response);
  36. break;
  37. case 3:
  38. this.findOrderItem(request, response);
  39. break;
  40. case 4:
  41. this.delete(request, response);
  42. break;
  43. }
  44. }
  45. // 添加订单和订单项
  46. public void addOrder(HttpServletRequest request,
  47. HttpServletResponse response) throws ServletException, IOException {
  48. /**
  49. * 1、判断用户是否登录,如果未登录就重定向到login.jsp 2、使用工具类将订单表中的信息封装成order对象,补全剩余的字段
  50. * 3、从session中获取购物车,遍历购物车,创建订单项,把它们加入list集合 4、添加订单和订单项
  51. */
  52. User user = (User) request.getSession().getAttribute("user");
  53. if (user == null) {
  54. response.sendRedirect("jsp/login.jsp");
  55. return;
  56. }
  57. Order order = CommonsUtils.toBean(request.getParameterMap(),
  58. Order.class);
  59. // 补全字段
  60. order.setId(CommonsUtils.uuid());
  61. order.setOrdertime(new Date());
  62. order.setPaysate(0);
  63. order.setUser_id(user.getId());
  64. Map<Product, Integer> cart = (Map<Product, Integer>) request
  65. .getSession().getAttribute("cart");
  66. List<OrderItem> list = new ArrayList<OrderItem>();
  67. for (Product p : cart.keySet()) {
  68. OrderItem orderItem = new OrderItem();
  69. orderItem.setId(CommonsUtils.uuid());
  70. orderItem.setOrder_id(order.getId());
  71. orderItem.setProduct_id(p.getId());
  72. orderItem.setBuynum(cart.get(p));
  73. orderItem.setSubtotal(p.getPrice() * orderItem.getBuynum());
  74. // 订单项加入list集合
  75. list.add(orderItem);
  76. }
  77. order.setList(list);
  78. // 添加订单和订单项
  79. String result = orderService.addOrder(order);
  80. request.setAttribute("orderResult", result);
  81. response.getWriter().print("购买成功,三秒后跳转到商品界面......");
  82. response.setHeader("Refresh", "3;url=/shopping/index.jsp");
  83. }
  84. // 查看我的订单
  85. public void findMyOrder(HttpServletRequest request,
  86. HttpServletResponse response) throws ServletException, IOException {
  87. /**
  88. * 1、用户已经登录,从session中获取用户信息,从用户信息中得到user_id 2、调用方法得到用户的订单的集合
  89. * 3、通过请求转发将订单集合显示在myorder.jsp页面上
  90. */
  91. User user = (User) request.getSession().getAttribute("user");
  92. String user_id = user.getId();
  93. List<Order> list = orderService.findMyOrder(user_id);
  94. request.setAttribute("myorderList", list);
  95. request.getRequestDispatcher("/jsp/myorder.jsp").forward(request,
  96. response);
  97. }
  98. // 查看我的订单的详情
  99. public void findOrderItem(HttpServletRequest request,
  100. HttpServletResponse response) throws ServletException, IOException {
  101. /**
  102. * 1、从请求转发中获取订单的id 2、调用方法得到订单详情的集合 3、通过请求转发将订单详情在orderItem.jsp页面上显示
  103. */
  104. String order_id = request.getParameter("order_id");
  105. System.out.println(order_id + "ssssss");
  106. List<OrderItem> list = orderService.findOrderItem(order_id);
  107. request.setAttribute("orderItemList", list);
  108. request.getRequestDispatcher("/jsp/orderitem.jsp").forward(request,
  109. response);
  110. }
  111. public void delete(HttpServletRequest request, HttpServletResponse response)
  112. throws ServletException, IOException {
  113. // 从请求中获取id
  114. String id = request.getParameter("id");
  115. // 执行删除客户业务
  116. orderService.delete(id);
  117. // 给客户端发送删除成功提示
  118. response.getWriter().print("删除成功,三秒后跳转到功能界面......");
  119. response.setHeader("Refresh", "3;url=/shopping/admin/list.jsp");
  120. }
  121. }

 

  1. package cn.edu.aynu.rjxy.servlet;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import cn.edu.aynu.rjxy.bean.Product;
  13. import cn.edu.aynu.rjxy.service.ProductService;
  14. /**
  15. * 调用ProductService的方法
  16. *
  17. * @author Administrator
  18. *
  19. */
  20. public class ProductServlet extends HttpServlet {
  21. private ProductService ps = new ProductService();
  22. public void doGet(HttpServletRequest request, HttpServletResponse response)
  23. throws ServletException, IOException {
  24. this.doPost(request, response);
  25. }
  26. public void doPost(HttpServletRequest request, HttpServletResponse response)
  27. throws ServletException, IOException {
  28. // 处理中文乱码
  29. request.setCharacterEncoding("UTF-8");
  30. response.setContentType("text/html;charset=UTF-8");
  31. // 获取隐藏字段method的值,并把它转换为int型
  32. int method = Integer.parseInt(request.getParameter("method"));
  33. switch (method) {
  34. case 1:
  35. this.findAll(request, response);
  36. break;
  37. case 2:
  38. this.addCart(request, response);
  39. break;
  40. }
  41. }
  42. // 处理查询所有商品的请求
  43. public void findAll(HttpServletRequest request, HttpServletResponse response)
  44. throws ServletException, IOException {
  45. List<Product> list = ps.findAll();
  46. request.setAttribute("listProduct", list);
  47. // 通过请求转发将商品信息显示在product.jsp页面上
  48. request.getRequestDispatcher("/jsp/product.jsp").forward(request,
  49. response);
  50. }
  51. // 处理添加购物车的请求
  52. public void addCart(HttpServletRequest request, HttpServletResponse response)
  53. throws ServletException, IOException {
  54. // 获取商品的id
  55. String id = request.getParameter("id");
  56. // 得到商品
  57. Product p = ps.findById(id);
  58. // 购物车是保存在session中
  59. HttpSession session = request.getSession();
  60. // 从session中拿到购物车
  61. Map<Product, Integer> cart = (Map<Product, Integer>) session
  62. .getAttribute("cart");
  63. // 如果cart不存在,就创建购物车
  64. if (cart == null) {
  65. cart = new HashMap<Product, Integer>();
  66. }
  67. /**
  68. * 遍历Map中的所有key也就是商品对象,如果发现有的商品的id和 即将加入购物车的id相同,就在原来的数量上+1
  69. */
  70. Iterator<Product> it = cart.keySet().iterator();
  71. boolean f = true;
  72. while (it.hasNext()) {
  73. Product pp = (Product) it.next();
  74. if (pp.getId().equals(p.getId())) {
  75. cart.put(pp, cart.get(pp) + 1);
  76. f = false;
  77. }
  78. }
  79. // 如果没有发现购物车原来相同的商品,就直接加入
  80. if (f) {
  81. cart.put(p, 1);
  82. }
  83. // 将cart放入session
  84. session.setAttribute("cart", cart);
  85. // 重定向
  86. response.sendRedirect("jsp/cart.jsp");
  87. }
  88. }
  1. package cn.edu.aynu.rjxy.servlet;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import cn.edu.aynu.rjxy.bean.User;
  9. import cn.edu.aynu.rjxy.exception.UserException;
  10. import cn.edu.aynu.rjxy.service.UserService;
  11. import cn.edu.aynu.rjxy.utils.CommonsUtils;
  12. /**
  13. * 调用UserService里面的方法
  14. *
  15. * @author Administrator
  16. *
  17. */
  18. public class UserServlet extends HttpServlet {
  19. private UserService userService = new UserService();
  20. public void doGet(HttpServletRequest request, HttpServletResponse response)
  21. throws ServletException, IOException {
  22. this.doPost(request, response);
  23. }
  24. public void doPost(HttpServletRequest request, HttpServletResponse response)
  25. throws ServletException, IOException {
  26. // 处理中文乱码
  27. request.setCharacterEncoding("UTF-8");
  28. response.setContentType("text/html;charset=UTF-8");
  29. // 获取隐藏字段method的值,并把它转换为int型
  30. int method = Integer.parseInt(request.getParameter("method"));
  31. switch (method) {
  32. case 1:
  33. this.register(request, response);
  34. break;
  35. case 2:
  36. this.login(request, response);
  37. break;
  38. }
  39. }
  40. public void register(HttpServletRequest request,
  41. HttpServletResponse response) throws ServletException, IOException {
  42. /**
  43. * 1、将注册表单中的用户信息封装到user对象中 2、将获取的UUID值作为user对象的id
  44. * 3、注册,如果发生异常,就到register.jsp页面上显示:用户已存在 如果成功,显示注册成功,3秒后就跳转到index.jsp页面。
  45. */
  46. User user = CommonsUtils.toBean(request.getParameterMap(), User.class);
  47. user.setId(CommonsUtils.uuid());
  48. try {
  49. userService.register(user);
  50. request.setAttribute("msg", "注册成功");
  51. request.getRequestDispatcher("/jsp/index.jsp").forward(request,
  52. response);
  53. } catch (UserException e) {
  54. request.setAttribute("msg", e.getMessage());
  55. // 将用户在注册表单中输入的信息保存在request域中,请求转发的register.jsp,目的回显
  56. request.setAttribute("user", user);
  57. request.getRequestDispatcher("/jsp/register.jsp").forward(request,
  58. response);
  59. }
  60. }
  61. public void login(HttpServletRequest request, HttpServletResponse response)
  62. throws ServletException, IOException {
  63. /**
  64. * 1、将用户登录表单中的信息封装成User对象 2、登录,如果发生异常,就转发到login.jsp页面,显示:异常信息
  65. * 如果成功,就将查询到的user对象u,存放到session域中 然后转发到shopping.jsp页面,显示:欢迎用户登录
  66. */
  67. User user = CommonsUtils.toBean(request.getParameterMap(), User.class);
  68. System.out.println(user + "-------->");
  69. try {
  70. User u = userService.login(user);
  71. System.out.println(u + ">>>>");
  72. // 将u存放到session域中
  73. request.getSession().setAttribute("user", u);
  74. request.getRequestDispatcher("/jsp/index.jsp").forward(request,
  75. response);
  76. } catch (UserException e) {
  77. request.setAttribute("msg", e.getMessage());
  78. request.setAttribute("user", user); // 回显
  79. request.getRequestDispatcher("/jsp/login.jsp").forward(request,
  80. response);
  81. }
  82. }
  83. }

 

  1. package cn.edu.aynu.rjxy.service;
  2. import java.sql.SQLException;
  3. import cn.edu.aynu.rjxy.bean.Admin;
  4. import cn.edu.aynu.rjxy.dao.AdminDao;
  5. public class AdminService {
  6. private AdminDao aDao = new AdminDao();
  7. /**
  8. * 根据用户名和密码查询用户
  9. *
  10. * @param cid
  11. */
  12. public Admin login(String username, String password) {
  13. try {
  14. return aDao.findByCid(username, password);
  15. } catch (SQLException e) {
  16. throw new RuntimeException(e);
  17. }
  18. }
  19. }
  1. package cn.edu.aynu.rjxy.service;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import cn.edu.aynu.rjxy.bean.Order;
  5. import cn.edu.aynu.rjxy.bean.OrderItem;
  6. import cn.edu.aynu.rjxy.dao.OrderDao;
  7. import cn.edu.aynu.rjxy.dao.OrderItemDao;
  8. import cn.edu.aynu.rjxy.utils.JDBCUtils;
  9. /**
  10. * 调用dao里面的方法
  11. *
  12. * @author Administrator
  13. *
  14. */
  15. public class OrderService {
  16. private OrderDao orderDao = new OrderDao();
  17. private OrderItemDao orderItemDao = new OrderItemDao();
  18. // 添加订单和订单项
  19. public String addOrder(Order order) {
  20. try {
  21. // 开启事物
  22. JDBCUtils.startTransaction();
  23. // 添加订单
  24. orderDao.add(order);
  25. // 获取订单项的集合
  26. List<OrderItem> list = order.getList();
  27. // 遍历订单项目,依次将它们插入orderitem表中
  28. for (OrderItem orderItem : list) {
  29. // 添加订单项
  30. orderItemDao.addItem(orderItem);
  31. }
  32. // 提交事务
  33. JDBCUtils.commit();
  34. return "success";
  35. } catch (Exception e) {
  36. // 回滚事务
  37. JDBCUtils.rollback();
  38. e.printStackTrace();
  39. return "fail";
  40. }
  41. }
  42. // 查看所有的订单
  43. public List<Order> findAllOrder() {
  44. try {
  45. return orderDao.findAllOrder();
  46. } catch (SQLException e) {
  47. throw new RuntimeException(e);
  48. }
  49. }
  50. // 查看我的订单
  51. public List<Order> findMyOrder(String user_id) {
  52. try {
  53. return orderDao.findOrderByUser_id(user_id);
  54. } catch (SQLException e) {
  55. throw new RuntimeException(e);
  56. }
  57. }
  58. // 查看某个订单
  59. public List<OrderItem> findOrderItem(String order_id) {
  60. try {
  61. return orderItemDao.findOrderItem(order_id);
  62. } catch (SQLException e) {
  63. throw new RuntimeException(e);
  64. }
  65. }
  66. // 删除某个订单
  67. public void delete(String id) {
  68. try {
  69. orderDao.deleteById(id);
  70. } catch (SQLException e) {
  71. throw new RuntimeException(e);
  72. }
  73. }
  74. }
  1. package cn.edu.aynu.rjxy.service;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import cn.edu.aynu.rjxy.bean.Page;
  5. import cn.edu.aynu.rjxy.bean.Product;
  6. import cn.edu.aynu.rjxy.dao.ProductDao;
  7. /**
  8. * 调用ProductDao的方法
  9. *
  10. * @author Administrator
  11. *
  12. */
  13. public class ProductService {
  14. private ProductDao pDao = new ProductDao();
  15. /**
  16. * 添加商品
  17. *
  18. * @param c
  19. */
  20. public void add(Product p) {
  21. try {
  22. pDao.insert(p);
  23. } catch (SQLException e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27. //上传照片
  28. public void upload(Product p,String id){
  29. try {
  30. pDao.insertURL(p, id);
  31. } catch (SQLException e) {
  32. throw new RuntimeException(e);
  33. }
  34. }
  35. /**
  36. * 删除客户业务
  37. */
  38. public void delete(String cid) {
  39. try {
  40. pDao.delete(cid);
  41. } catch (SQLException e) {
  42. throw new RuntimeException(e);
  43. }
  44. }
  45. /**
  46. * 编辑修改商品
  47. *
  48. * @param c
  49. */
  50. public void edit(Product p) {
  51. try {
  52. pDao.update(p);
  53. } catch (SQLException e) {
  54. throw new RuntimeException(e);
  55. }
  56. }
  57. // 查询所有商品
  58. public List<Product> findAll() {
  59. try {
  60. return pDao.findAll();
  61. } catch (SQLException e) {
  62. throw new RuntimeException(e);
  63. }
  64. }
  65. // 查看某一商品
  66. public Product findById(String id) {
  67. try {
  68. return pDao.findProductById(id);
  69. } catch (SQLException e) {
  70. throw new RuntimeException(e);
  71. }
  72. }
  73. /**
  74. * 分页查询
  75. *
  76. * @param pageCode
  77. * @return
  78. */
  79. public Page queryByPage(int pageCode) {
  80. try {
  81. // 获取总记录数
  82. int totalRecord = pDao.queryTotalRecord();
  83. // 创建Page
  84. Page pb = new Page(pageCode, totalRecord);
  85. // 查询当前页的记录
  86. List<Product> datas = pDao.queryByPage((pageCode - 1)
  87. * pb.getPageSize(), pb.getPageSize());
  88. // 将datas封装到pb
  89. pb.setDatas(datas);
  90. return pb;
  91. } catch (SQLException e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. }
  1. package cn.edu.aynu.rjxy.service;
  2. import java.sql.SQLException;
  3. import cn.edu.aynu.rjxy.bean.User;
  4. import cn.edu.aynu.rjxy.dao.UserDao;
  5. import cn.edu.aynu.rjxy.exception.UserException;
  6. /**
  7. * 调用dao层的方法
  8. *
  9. * @author Administrator
  10. *
  11. */
  12. public class UserService {
  13. private UserDao userDao = new UserDao();
  14. /**
  15. * 用户注册 1、检测输入的用户名是否存在,如果存在抛出异常“用户已存在” 2、把输入的用户添加到数据库中的user表
  16. *
  17. * @throws UserException
  18. */
  19. public void register(User user) throws UserException {
  20. try {
  21. // 检测用户是否存在
  22. User u = userDao.findByUsername(user.getUsername());
  23. // 如果存在抛出异常否则添加用户到user表
  24. if (u != null) {
  25. throw new UserException("用户已经存在");
  26. } else {
  27. userDao.add(user);
  28. }
  29. } catch (SQLException e) {
  30. throw new RuntimeException(e);
  31. }
  32. }
  33. /**
  34. * 用户登录
  35. *
  36. * @throws UserException
  37. */
  38. public User login(User user) throws UserException {
  39. try {
  40. // 根据用户名查询user表得到user对象u
  41. User u = userDao.findByUsername(user.getUsername());
  42. /**
  43. * 如果u为null,说明用户不存在,抛出异常,显示用户不存在 否则说明用户存在,检测u对象中的密码和用户输入的面貌是否一致
  44. * 如果一致,返回查询到的u否则抛出异常,显示密码错误
  45. */
  46. if (u == null) {
  47. throw new UserException("用户名不存在");
  48. }
  49. // 查询到的用户密码和用户名输入的密码不一致
  50. if (!u.getPassword().equals(user.getPassword())) {
  51. throw new UserException("密码错误");
  52. }
  53. return u;
  54. } catch (SQLException e) {
  55. throw new RuntimeException(e);
  56. }
  57. }
  58. }
  1. package cn.edu.aynu.rjxy.dao;
  2. import java.sql.SQLException;
  3. import org.apache.commons.dbutils.QueryRunner;
  4. import org.apache.commons.dbutils.handlers.BeanHandler;
  5. import cn.edu.aynu.rjxy.bean.Admin;
  6. import cn.edu.aynu.rjxy.utils.JDBCUtils;
  7. /**
  8. * 对管理员表的操作
  9. *
  10. * @author Administrator
  11. *
  12. */
  13. public class AdminDao {
  14. QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
  15. /**
  16. * 根据用户名和用户密码查询
  17. *
  18. * @return
  19. * @throws SQLException
  20. */
  21. public Admin findByCid(String username, String password)
  22. throws SQLException {
  23. String sql = "select * from admin where username=?&&password=?";
  24. Admin admin = qr.query(sql, new BeanHandler<Admin>(Admin.class),
  25. username, password);
  26. return admin;
  27. }
  28. }
  1. package cn.edu.aynu.rjxy.dao;
  2. import java.sql.Connection;
  3. import java.sql.Date;
  4. import java.sql.SQLException;
  5. import java.util.List;
  6. import org.apache.commons.dbutils.QueryRunner;
  7. import org.apache.commons.dbutils.handlers.BeanListHandler;
  8. import cn.edu.aynu.rjxy.bean.Order;
  9. import cn.edu.aynu.rjxy.utils.JDBCUtils;
  10. /**
  11. * 对order1表的操作
  12. *
  13. * @author Administrator
  14. *
  15. */
  16. public class OrderDao {
  17. private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
  18. // 添加订单到数据库表order1中
  19. public void add(Order order) throws Exception {
  20. Connection conn = JDBCUtils.getConnection();
  21. String sql = "insert into order1 values(?,?,?,?,?,?,?,?)";
  22. // 订单生成时间,由java.util.Date----->java.sql.Date
  23. Date date = new Date(order.getOrdertime().getTime());
  24. qr.update(conn, sql, order.getId(), order.getTotalMoney(), order
  25. .getReceiverAddress(), order.getReceiverName(), order
  26. .getReceiverPhone(), order.getPaysate(), date, order
  27. .getUser_id());
  28. }
  29. // 查看所有用户的订单
  30. public List<Order> findAllOrder() throws SQLException {
  31. String sql = "select * from order1";
  32. return qr.query(sql, new BeanListHandler<Order>(Order.class));
  33. }
  34. // 根据user_id查看用户的订单
  35. public List<Order> findOrderByUser_id(String user_id) throws SQLException {
  36. String sql = "select * from order1 where user_id = ?";
  37. return qr.query(sql, new BeanListHandler<Order>(Order.class), user_id);
  38. }
  39. // 根据user_id删除用户的订单
  40. public void deleteById(String id) throws SQLException {
  41. String sql = "delete from order1 where id=?";
  42. qr.update(sql, id);
  43. }
  44. }
  1. package cn.edu.aynu.rjxy.dao;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.util.List;
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7. import cn.edu.aynu.rjxy.bean.OrderItem;
  8. import cn.edu.aynu.rjxy.bean.Product;
  9. import cn.edu.aynu.rjxy.utils.JDBCUtils;
  10. /**
  11. * 对orderItem表的操作
  12. *
  13. * @author Administrator
  14. *
  15. */
  16. public class OrderItemDao {
  17. private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
  18. // 添加订单项到数据库表orderitem中
  19. public void addItem(OrderItem orderItem) throws Exception {
  20. Connection conn = JDBCUtils.getConnection();
  21. String sql = "insert into orderitem values(?,?,?,?,?)";
  22. qr.update(conn, sql, orderItem.getId(), orderItem.getOrder_id(),
  23. orderItem.getProduct_id(), orderItem.getBuynum(), orderItem
  24. .getSubtotal());
  25. }
  26. // 查询某一个订单的所有订单项
  27. public List<OrderItem> findOrderItem(String order_id) throws SQLException {
  28. // 查询某一订单所包含的订单项
  29. String sql = "select * from orderitem where order_id = ?";
  30. List<OrderItem> orderItemList = qr.query(sql,
  31. new BeanListHandler<OrderItem>(OrderItem.class), order_id);
  32. // 查询某一订单所包含的订单项对应的商品信息
  33. sql = "select p.id,p.name,p.price from product p,orderitem oi where p.id = oi.product_id and order_id = ?";
  34. List<Product> productList = qr.query(sql, new BeanListHandler<Product>(
  35. Product.class), order_id);
  36. // 将查询出来的商品作为订单项的属性进行添加
  37. for (OrderItem oi : orderItemList) {
  38. for (Product p : productList) {
  39. if (oi.getProduct_id().equals(p.getId())) {
  40. oi.setProduct(p);
  41. }
  42. }
  43. }
  44. return orderItemList;
  45. }
  46. }
  1. package cn.edu.aynu.rjxy.dao;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import org.apache.commons.dbutils.QueryRunner;
  5. import org.apache.commons.dbutils.handlers.BeanHandler;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7. import org.apache.commons.dbutils.handlers.ScalarHandler;
  8. import cn.edu.aynu.rjxy.bean.Product;
  9. import cn.edu.aynu.rjxy.utils.JDBCUtils;
  10. /**
  11. * 对product表进行增删改查
  12. *
  13. * @author Administrator
  14. *
  15. */
  16. public class ProductDao {
  17. QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
  18. /**
  19. * 添加商品
  20. *
  21. * @param c
  22. * @throws SQLException
  23. */
  24. public void insert(Product p) throws SQLException {
  25. String sql = "insert into product(id,name,price,category,pnum,description) values(?,?,?,?,?,?)";
  26. qr.update(sql, p.getId(), p.getName(), p.getPrice(), p.getCategory(), p
  27. .getPnum(), p.getDescription());
  28. }
  29. public void insertURL(Product p,String id) throws SQLException {
  30. String sql = "update product set imgurl = ? where id =?";
  31. qr.update(sql, p.getImgurl(),id);
  32. }
  33. // 查询所有商品
  34. public List<Product> findAll() throws SQLException {
  35. String sql = "select * from product";
  36. List<Product> list = qr.query(sql, new BeanListHandler<Product>(
  37. Product.class));
  38. return list;
  39. }
  40. // 根据id查找商品
  41. public Product findProductById(String id) throws SQLException {
  42. String sql = "select * from product where id=?";
  43. return qr.query(sql, new BeanHandler<Product>(Product.class), id);
  44. }
  45. /**
  46. * 根据传过来的id,删除商品
  47. *
  48. * @throws SQLException
  49. *
  50. */
  51. public void delete(String id) throws SQLException {
  52. String sql = "delete from product where id=?";
  53. qr.update(sql, id);
  54. }
  55. /**
  56. * 修改商品属性
  57. *
  58. * @throws SQLException
  59. */
  60. public void update(Product p) throws SQLException {
  61. String sql = "update product set name=?,price=?,category=?,pnum=?,imgurl=?,description=? where id=?";
  62. qr.update(sql, p.getName(), p.getPrice(), p.getCategory(), p.getPnum(),
  63. p.getImgurl(), p.getDescription(), p.getId());
  64. }
  65. /**
  66. * 查询总记录数
  67. *
  68. * @throws SQLException
  69. *
  70. */
  71. public int queryTotalRecord() throws SQLException {
  72. String sql = "select count(*) from product";
  73. Long num = qr.query(sql, new ScalarHandler<Long>());
  74. return num.intValue();
  75. }
  76. /**
  77. * 分页查询
  78. *
  79. * @throws SQLException
  80. *
  81. */
  82. public List<Product> queryByPage(int recordStartIndex, int pageSize)
  83. throws SQLException {
  84. String sql = "select * from product limit ?,?";
  85. List<Product> datas = qr.query(sql, new BeanListHandler<Product>(
  86. Product.class), recordStartIndex, pageSize);
  87. return datas;
  88. }
  89. }
  1. package cn.edu.aynu.rjxy.dao;
  2. import java.sql.SQLException;
  3. import org.apache.commons.dbutils.QueryRunner;
  4. import org.apache.commons.dbutils.handlers.BeanHandler;
  5. import cn.edu.aynu.rjxy.bean.User;
  6. import cn.edu.aynu.rjxy.utils.JDBCUtils;
  7. /**
  8. * 对User表的操作
  9. *
  10. * @author Administrator
  11. *
  12. */
  13. public class UserDao {
  14. QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
  15. /**
  16. * 添加用户到user表
  17. *
  18. * @throws SQLException
  19. */
  20. public void add(User user) throws SQLException {
  21. String sql = "insert into user(id,username,password) values(?,?,?)";
  22. qr.update(sql, user.getId(), user.getUsername(), user.getPassword());
  23. }
  24. /**
  25. * 通过用户名查找用户
  26. *
  27. * @throws SQLException
  28. */
  29. public User findByUsername(String username) throws SQLException {
  30. String sql = "select * from user where username = ?";
  31. User user = qr.query(sql, new BeanHandler<User>(User.class), username);
  32. return user;
  33. }
  34. }

http://lijun0349-163-com.iteye.com/blog/2008680

http://blog.sina.com.cn/s/blog_ae96abfd0101p9bf.html

版权声明:本文为kangyaping原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/kangyaping/p/6165842.html