Android网络编程之一个Android下菜单系统模块的实现(服务器端—结算功能(上部))
完成了下单功能后,我们来编写结算功能模块,结算的主要流程为:
客户端传送主订单号 ---> 服务器端接收请求 ---> 根据主订单号取出所有与该订单有关的信息 ---> 将其编写为html 传回客户端的WebView直接加载显示
这一部分的服务器端实现分为上下两个部分,第一部分我们先让服务器返回主订单信息显示。
我们依然使用DAO设计来实现,首先设计出一个实体类来封装根据对应订单号查出的主订单信息:
com.moka.entity中新建QueryOrder.java:
/** * 用于封装查一个对应订单的信息的实体类 */ public class QueryOrder { // 服务员名字 private String name; // 下单时间 private String orderTime; // 人数 private int personNum; // 桌号 private int tableId; getters & setters .. }
然后,设计操作这些数据的DAO与其实现类:
public interface PayDao { // 取得主订单信息 public QueryOrder getOrderById(int id); }
public class PayDaoImpl implements PayDao { @Override public QueryOrder getOrderById(int id) { // jdbc连接数据库准备工作 DBUtil util = new DBUtil(); Connection conn = util.openConnection(); // 插入数据库操作用,此处用到表连接,表重命名的知识 String queryOrderSql = "select ut.name, ot.orderTime, ot.personNum, ot.tableId from ordertbl ot " + "join usertbl ut on ot.userId = ut.id where ot.id = ?" ; PreparedStatement ps = null; ResultSet resultSet = null; // 实例化封装数据的QueryOrder类 QueryOrder queryOrder = new QueryOrder(); try { ps = conn.prepareStatement(queryOrderSql); ps.setInt(1, id); resultSet = ps.executeQuery(); if (resultSet.next()) { // 使用所获数据填充QueryOrder queryOrder.setName(resultSet.getString(1)); queryOrder.setOrderTime(resultSet.getString(2)); queryOrder.setPersonNum(resultSet.getInt(3)); queryOrder.setTableId(resultSet.getInt(4)); } } 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 queryOrder; } }
再写QueryOrderServlet:
public class QueryOrderServlet extends HttpServlet { private static final long serialVersionUID = -8119485282580908498L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintStream ps = new PrintStream(resp.getOutputStream(), true, "utf-8"); // 根据传来的orderId得到对应封装数据 String id = req.getParameter("id"); int orderId = Integer.parseInt(id); PayDao payDao = new PayDaoImpl(); QueryOrder order = payDao.getOrderById(orderId); // 从封装数据包中取出数据 String name = order.getName(); String orderTime = order.getOrderTime(); int personNum = order.getPersonNum(); int tableId = order.getTableId(); // 将数据写进Html页面传回客户端 ps.println("<HTML>"); ps.println(" <HEAD></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(name); 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("</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); } }
最后配置servlet:
<servlet> <servlet-name>QueryOrderServlet</servlet-name> <servlet-class>com.moka.servlet.QueryOrderServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueryOrderServlet</servlet-name> <url-pattern>/servlet/QueryOrderServlet</url-pattern> </servlet-mapping>