ManageSerlvlet

public class MangeServlet extends HttpServlet {


    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            String reqtype = request.getParameter("type");
            //out.print(reqtype);
            Connection cn = (new Conn()).getConn();;
            ResultSet rs = null;
            if (reqtype.equals("0")) {
                String myselected = request.getParameter("select_good");
                String mysel = request.getParameter("sel");
                String mysql = null;
                if (myselected.equals("1")) {
                    mysql = "select * from goods where id = '" + mysel + "'";
                } else if (myselected.equals("2")) {
                    mysql = "select * from goods where name = '" + mysel + "'";
                } else if (myselected.equals("3")) {
                    mysql = "select * from goods where price = '" + mysel + "'";
                }
                try {                   
                    Statement myst = cn.createStatement();
                    ResultSet myrs = myst.executeQuery(mysql);
                    ArrayList<Productbean> products = new ArrayList<>();
                    while(myrs.next()){
                        Productbean p = new Productbean();
                        p.setId(myrs.getString(1));
                        p.setName(myrs.getString(2));
                        p.setNum(myrs.getString(3));
                        p.setDes(myrs.getString(4));
                        products.add(p);                      
                    }
                    request.setAttribute("sp", products);
                     request.getRequestDispatcher("goods.jsp").forward(request, response);
                } catch (SQLException ex) {
                }
            }
            if (reqtype.equals("1")) {
                String myid = request.getParameter("add_goodID");
                String mygood = request.getParameter("add_good");
                String mynum = request.getParameter("add_goodNum");
                String mydes = request.getParameter("add_goodDes");
                String mysql0 = "select * from goods where id = '" + myid + "'";
                String mysql = "insert into goods values('" + myid + "','" + mygood + "','" + mynum + "','" + mydes + "')";


                try {
                    Statement myst = cn.createStatement();
                    if (myst.executeQuery(mysql0).next()) {
                        out.println("<html><script>alert('此产品序号已经存在,请重新输入!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
                    } else {
                        myst.execute(mysql);
                        out.println("<html><script>alert('添加成功!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
                    }
                    myst.execute(mysql);
                    response.sendRedirect("goods.jsp");
                } catch (SQLException ex) {
                }
            }
            if (reqtype.equals("2")) {
                String myid = request.getParameter("del_goodID");
                String mysql = "select * from goods where id = '" + myid + "'";
                String mysql0 = "delete from goods where id ='" + myid + "'";
                //
                try {
                    Statement myst = cn.createStatement();
                    if (myst.executeQuery(mysql).next()) {
                        myst.execute(mysql0);
                        out.println("<html><script>alert('删除成功!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");
                    } else {
                        out.println("<html><script>alert('不存在这个香水,请重新输入!');setTimeout(function(){window.location.href='goods.jsp';},1);</script></html>");


                    }


                } catch (SQLException ex) {


                }
            }
            if (reqtype.equals("3")) {
                // out.println("<script>alert('是否删除'));</script>");
                String myid = request.getParameter("mod_goodID");
                String mygood = request.getParameter("mod_good");
                String mynum = request.getParameter("mod_goodNum");
                String mydes = request.getParameter("mod_goodDes");
                String mysql = "update goods set name='" + mygood + "',number='" + mynum + "',des ='" + mydes + "' where id='" + myid + "' ";
                System.out.println(mysql);
                try {
                    Statement myst = cn.createStatement();
                    myst.execute(mysql);
                    response.sendRedirect("goods.jsp");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值