zoukankan      html  css  js  c++  java
  • Android网络编程之一个Android下菜单系统模块的实现(服务器端—结算功能(中部))

    这次我们向主订单信息下面添加订单明细信息,也是遵循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 ..
        
    }
    View Code

    在上次写好的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;
        }
    View Code

    然后在上次写好的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);
        }
    
    }
    View Code

    拿第20张订单试验,实现后,返回客户端的效果如图:

  • 相关阅读:
    SpringBoot + redis + @Cacheable注解实现缓存清除缓存
    Linux常用命令
    Java8 Stream分组
    VMware CentOS网络配置(局域网其它主机可访问Linux虚拟机)
    Jenkins实现自动打包,MAVEN打包,Shell脚本启动
    Docker常用命令,Docker安装Nginx、Redis、Jenkins、tomcat、MySQL
    Postman配置Pre-request scripts预请求对请求进行AES加密
    《Java并发编程的艺术》并发编程的基础(四)
    linux shell的创建与启动
    《Java并发编程的艺术》Java内存模型(三)
  • 原文地址:https://www.cnblogs.com/moka/p/3083403.html
Copyright © 2011-2022 走看看