在让数学来计算while循环中的借方和贷方时遇到麻烦

时间:2018-12-04 02:14:15

标签: java loops servlets

我几乎已经完成了该程序,但是我需要它来显示基于此晚餐俱乐部交易记录上当前的贷方和借方所欠的总余额。我相信应该在我发布的代码中的while循环中/附近。我的代码已附加。

public class ShowPurchasesServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String URL="/MemberScreen.jsp";
        String sql="", msg="", mo="", dy="", yr="", sqlwhere="";

        try {
            Member m = (Member) request.getSession().getAttribute("m");
            mo = request.getParameter("month");
            dy = request.getParameter("day");
            yr = request.getParameter("year");
            if (mo.isEmpty() || dy.isEmpty() || yr.isEmpty()) {
                sqlwhere = "";
            } else {
                sqlwhere = yr + " " + mo + " " + dy;
            }

            sql = "SELECT p.MemID, p.PurchaseDt, p.TransType, p.TransCd, " +
                    " c.TransDesc, p.amount" + 
                    " FROM tblPurchases p, tblCodes c " +
                    " WHERE p.TransCd = c.TransCd " + 
                    "   AND p.MemID = ? "; 
            if  (!sqlwhere.isEmpty()) {
                sql += " AND p.PurchaseDt >= ? ";
            } 
            sql += " ORDER BY p.PurchaseDt ";

            ConnectionPool pool = ConnectionPool.getInstance();
            Connection conn = pool.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, m.getMemid());
            if (!sqlwhere.isEmpty()) {
                ps.setString(2, sqlwhere);
            }

            ResultSet r = ps.executeQuery();

            ArrayList<Purchase> pur = new ArrayList<>();
            while (r.next()) {
                Purchase p = new Purchase();
                p.setMemid(m.getMemid());
                p.setPurchasedt(r.getString("PurchaseDt"));
                p.setTranscd(r.getString("TransCd"));
                p.setTransdesc(r.getString("TransDesc"));
                p.setAmount(r.getDouble("Amount"));
                pur.add(p);
            }
            request.setAttribute("pur", pur); 
            r.last();
            msg += "Member records = " + r.getRow() + "<br>";

            r.close();
            pool.freeConnection(conn);
            URL = "/Purchases.jsp";

        } catch (SQLException e) {
            msg += "SQL Error: " + e.getMessage() + "<br>";
        } catch (Exception e) {
            msg += "Servlet error: " + e.getMessage() + "<br>";
        }
            request.setAttribute("msg", msg);
              RequestDispatcher disp =
                getServletContext().getRequestDispatcher(URL);
        disp.forward(request, response);
    }

0 个答案:

没有答案