Android网络编程之一个Android下菜单系统模块的实现(服务器端—结算功能(下部))

接下来是最后一步,客户端点击“结算”按钮,服务器端需要需要做两件事:

1. 把此订单的isPay字段置位,表示此订单已经结算。

2. 把此订单对应的桌子的flag字段清零,表示已经没人占用此桌。

要实现第一点,我们在PayDao与PayDaoImpl中添加一个方法pay():

@Override
    public void pay(int id) {
        
        // jdbc连接数据库准备工作
        DBUtil util = new DBUtil(); 
        Connection conn = util.openConnection();
        
        // 更新数据库操作用
        String sql = "update ordertbl set isPay = 1 where id = ?" ;
        PreparedStatement ps = null;
        
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
View Code

要实现第二点,要找到我们之前在添菜功能中写的OrderDaoImpl中添加一个updateTableStatusEx()方法:

    @Override
    public void updateTableStatusEx(int orderId) {
        // jdbc连接数据库准备工作
        DBUtil util = new DBUtil(); 
        Connection conn = util.openConnection();
        
        // 更新此桌状态操作用
        String updateSql = "update tabletbl set flag = 0 where id = (select tableId from ordertbl where id = ?)";
        PreparedStatement ps = null;
        try {
            // 此桌已结账, table表中flag清零
            ps = conn.prepareStatement(updateSql);
            ps.setInt(1, orderId);
            ps.executeUpdate();
            
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                    ps = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
View Code

然后新建一个servlet名为PayServlet专门处理结算请求:

public class PayServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter pw = new PrintWriter(resp.getOutputStream());
        // 从客户端请求中取得订单号
        String id = req.getParameter("id");
        int orderId = Integer.parseInt(id);
        
        // ordertbl中isPay置位, 表示已经付款
        PayDao payDao = new PayDaoImpl();
        payDao.pay(orderId);
        
        // tabletbl中对应桌号flag位清零, 表示桌位已空
        OrderDao orderDao = new OrderDaoImpl();
        orderDao.updateTableStatusEx(orderId);
        
        pw.print("结算成功");
        pw.flush();
        pw.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }

}
View Code

最后注册servlet:

<servlet>
    <servlet-name>PayServlet</servlet-name>
    <servlet-class>com.moka.servlet.PayServlet</servlet-class>
</servlet>
     
<servlet-mapping>
    <servlet-name>PayServlet</servlet-name>
    <url-pattern>/servlet/PayServlet</url-pattern>
</servlet-mapping>

至此,结算功能模块服务器端编写完毕,下篇将编写客户端的操作

原文地址:https://www.cnblogs.com/moka/p/3083406.html