这次我们向主订单信息下面添加订单明细信息,也是遵循DAO模式,然后将数据拼成html传回客户端
com.moka.entity中新建一个实体类QueryOrderDetail:
/** * 用于封装查一个对应订单的明细信息的实体类 */ public class QueryOrderDetail { // 菜名 private String dishName; // 菜价 private int price; // 数量 private int dishNum; // 总价 private int totalPrice; // 备注 private String remark; getters & setters .. }
在上次写好的PayDao与PayDaoImpl中添加一个新方法getOrderDetailById():
public interface PayDao { // 取得主订单信息 public QueryOrder getOrderById(int id); // 取得详细订单信息 public List<QueryOrderDetail> getOrderDetailById(int id); }
@Override public List getOrderDetailById(int id) { // jdbc连接数据库准备工作 DBUtil util = new DBUtil(); Connection conn = util.openConnection(); // 插入数据库操作用,使用union合并最后单算出来的总价 String queryOrderDetailSql = "select mt.name, mt.price, odt.num, mt.price * odt.num totalPrice, odt.remark from orderdetailtbl odt " + "join menutbl mt on odt.menuId = mt.id where odt.orderId = ? " + "union select '', '', '', sum(mt.price * odt.num) allPrice, '' from orderdetailtbl odt " + "join menutbl mt on odt.menuId = mt.id where odt.orderId = ? " ; PreparedStatement ps = null; ResultSet resultSet = null; // 准备传回的QueryOrderDetail列表 List<QueryOrderDetail> list = new ArrayList<QueryOrderDetail>(); try { ps = conn.prepareStatement(queryOrderDetailSql); ps.setInt(1, id); ps.setInt(2, id); resultSet = ps.executeQuery(); while (resultSet.next()) { // 实例化封装数据的QueryOrderDetail类 QueryOrderDetail queryOrderDetail = new QueryOrderDetail(); // 使用所获数据填充QueryOrderDetail queryOrderDetail.setDishName(resultSet.getString(1)); queryOrderDetail.setPrice(resultSet.getInt(2)); queryOrderDetail.setDishNum(resultSet.getInt(3)); queryOrderDetail.setTotalPrice(resultSet.getInt(4)); queryOrderDetail.setRemark(resultSet.getString(5)); // 装入List list.add(queryOrderDetail); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (resultSet != null) { resultSet.close(); resultSet = null; } if (ps != null) { ps.close(); ps = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } return list; }
然后在上次写好的QueryOrderServlet中添加对应代码,拼成html:
public class QueryOrderServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter ps = new PrintWriter(resp.getOutputStream()); // 取得传来的orderId String id = req.getParameter("id"); int orderId = Integer.parseInt(id); // 获取PayDao实例 PayDao payDao = new PayDaoImpl(); // 得到主订单数据实例 QueryOrder order = payDao.getOrderById(orderId); // 得到订单明细数据实例列表 List<QueryOrderDetail> list = payDao.getOrderDetailById(orderId); // 从主订单数据实例中取出数据 String waiterName = order.getName(); String orderTime = order.getOrderTime(); int personNum = order.getPersonNum(); int tableId = order.getTableId(); // 将数据写进Html页面传回客户端 ps.println("<HTML>"); // 注:下面一句中head标签内要添加一条元数据,声明字符集为utf-8, 否则传回中文会显示乱码 ps.println(" <HEAD><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></HEAD>"); ps.println(" <BODY>"); ps.print("<table>"); ps.print("<tr>"); ps.print("<th>"); ps.print("Order ID"); ps.print("</th>"); ps.print("<th>"); ps.print("Waiter Name"); ps.print("</th>"); ps.print("<th>"); ps.print("Order Time"); ps.print("</th>"); ps.print("<th>"); ps.print("Person Num"); ps.print("</th>"); ps.print("<th>"); ps.print("Table ID"); ps.print("</th>"); ps.print("</tr>"); ps.print("<tr>"); ps.print("<td>"); ps.print(id); ps.print("</td>"); ps.print("<td>"); ps.print(waiterName); ps.print("</td>"); ps.print("<td>"); ps.print(orderTime); ps.print("</td>"); ps.print("<td>"); ps.print(personNum); ps.print("</td>"); ps.print("<td>"); ps.print(tableId); ps.print("</td>"); ps.print("</tr>"); ps.print("<tr>"); ps.print("<th>"); ps.print("Dish Name"); ps.print("</th>"); ps.print("<th>"); ps.print("Price"); ps.print("</th>"); ps.print("<th>"); ps.print("Dishes Num"); ps.print("</th>"); ps.print("<th>"); ps.print("Total Price"); ps.print("</th>"); ps.print("<th>"); ps.print("Remark"); ps.print("</th>"); ps.print("</tr>"); // 循环取出所有明细数据,拼成html Iterator<QueryOrderDetail> iterator = list.iterator(); while (iterator.hasNext()) { QueryOrderDetail detail = iterator.next(); String dishName = detail.getDishName(); int price = detail.getPrice(); int dishNum = detail.getDishNum(); int totalPrice = detail.getTotalPrice(); String remark = detail.getRemark(); ps.print("<tr>"); ps.print("<td>"); ps.print(dishName); ps.print("</td>"); ps.print("<td>"); ps.print(price); ps.print("</td>"); ps.print("<td>"); ps.print(dishNum); ps.print("</td>"); ps.print("<td>"); ps.print(totalPrice); ps.print("</td>"); ps.print("<td>"); ps.print(remark); ps.print("</td>"); ps.print("</tr>"); } ps.print("</table>"); ps.println(" </BODY>"); ps.println("</HTML>"); ps.flush(); ps.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
拿第20张订单试验,实现后,返回客户端的效果如图: