private void PasserCommande(Film film, int nb, int typ) { CallableStatement csmt = null; Statement smt = null; String request = "{ call PACKAGE_CC1.InsertCommande(?,?,?,?) }"; String type = (typ == 0 ? "F" : "T"); BeanAccesSQL DBSQL = ConnectionBD(); try { csmt = DBSQL.getConnection().prepareCall(request); csmt.setInt(1, film.getId()); csmt.setString(2, "CC1"); csmt.setInt(3, nb); csmt.setString(4, type); csmt.executeUpdate(); smt = DBSQL.getConnection().createStatement(); smt.executeQuery("commit"); } catch (SQLException ex) { Logger.getLogger(ServletEvent.class.getName()).log(Level.SEVERE, null, ex); try { smt.executeQuery("rollback"); } catch (SQLException ex1) { Logger.getLogger(ServletEvent.class.getName()).log(Level.SEVERE, null, ex1); } } finally { try { csmt.close(); smt.close(); } catch (SQLException ex) { } } }
/** * 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"); PrintWriter out = response.getWriter(); startThreadTimeOut(request); String action = request.getParameter("action"); User user = (User) request.getSession().getAttribute("Identification"); if (user != null) { if (action.equals("Recherche")) { getServletContext() .getRequestDispatcher("/JSPGlobalRecherche.jsp") .forward(request, response); } else if (action.equals("AddFilm")) { Film film = null; boolean verif = false; String name = recupName(Integer.parseInt(request.getParameter("id"))); int id = Integer.parseInt(request.getParameter("id")); Caddie caddie = (Caddie) request.getSession(true).getAttribute("Caddie"); for (int i = 0; i < caddie.getCaddie().size(); i++) { if (caddie.getCaddie().get(i).getId() == id) verif = true; } if (verif == true) { for (int i = 0; i < caddie.getCaddie().size(); i++) { if (caddie.getCaddie().get(i).getId() == id) caddie.getCaddie().get(i).setNbPhys(caddie.getCaddie().get(i).getNbPhys() + 1); } } else { film = new Film(id, name); caddie.getCaddie().add(film); film.setNbPhys(film.getNbPhys() + 1); } getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("RemoveCaddie")) { int id = Integer.parseInt(request.getParameter("id")); Caddie caddie = (Caddie) request.getSession(true).getAttribute("Caddie"); for (int i = 0; i < caddie.getCaddie().size(); i++) { if (caddie.getCaddie().get(i).getId() == id) caddie.getCaddie().remove(i); } getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("Caddie")) { getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("Logout")) { request.getSession().invalidate(); getServletContext().getRequestDispatcher("/").forward(request, response); } else if (action.equals("Error")) { getServletContext() .getRequestDispatcher("/JSPError.jsp?Message=Vous n'êtes pas identifié!") .forward(request, response); } else if (action.equals("addDelCopie")) { int id = Integer.parseInt(request.getParameter("id")); int type = Integer.parseInt(request.getParameter("type")); int signe = Integer.parseInt(request.getParameter("signe")); // type -> 0 = num / 1 = phys // signe -> 1 = + / -1 = - -> (+1*signe) Caddie caddie = (Caddie) request.getSession().getAttribute("Caddie"); ActionCadie(id, type, signe, caddie); getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("supprimerTout")) { Caddie caddie = (Caddie) request.getSession(true).getAttribute("Caddie"); caddie.viderCaddie(); getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("pageSuivante")) { String page = (String) request.getSession(true).getAttribute("pageCourante"); Caddie caddie = (Caddie) request.getSession(true).getAttribute("Caddie"); int numPage; if (page == null) { request.getSession(true).setAttribute("pageCourante", "1"); numPage = 1; } else { numPage = Integer.parseInt(page); } int max = (numPage * 5); if (max < caddie.getCaddie().size()) { numPage++; request.getSession(true).setAttribute("pageCourante", "" + numPage); getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("pagePrecedente")) { String page = (String) request.getSession(true).getAttribute("pageCourante"); int numPage; if (page == null) { request.getSession(true).setAttribute("pageCourante", "1"); numPage = 1; } else { numPage = Integer.parseInt(page); } if (numPage != 1) { numPage--; request.getSession(true).setAttribute("pageCourante", "" + numPage); getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } else if (action.equals("commander")) { Caddie caddie = (Caddie) request.getSession(true).getAttribute("Caddie"); for (Film film : caddie.getCaddie()) { if (film.getNbNum() > 0) PasserCommande(film, film.getNbNum(), 0); if (film.getNbPhys() > 0) PasserCommande(film, film.getNbPhys(), 1); } caddie.viderCaddie(); getServletContext().getRequestDispatcher("/JSPCaddie.jsp").forward(request, response); } } }