SHARE
TWEET

Untitled

a guest Mar 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public String updateSparePart(SparePart sparePart) throws Exception {
  2.         Connection conn = DBConnection.createConnection();
  3.         PreparedStatement spQuery = null;
  4.  
  5.         conn.setAutoCommit(false);
  6. //        String sql = "UPDATE spare_part SET part_name = '" + sparePart.getPartName() + "', vehicle_type = '" + sparePart.getVehicle_type() + "', manufacture = '" + sparePart.getManufacture() + "',price = '" + sparePart.getPrice() + "', stock_level  = '"
  7. //                +sparePart.getStockLevel() + "', threshold = '" + sparePart.getThreshold()+ "'   WHERE code = '" + sparePart.getCode() + "' ";
  8.  
  9.         spQuery = conn.prepareStatement("UPDATE spare_part SET part_name = ?," + "vehicle_type = ?,"
  10.                 + "manufacture = ?," + "price = ?," + "stock_level = ?, " + "threshold = ? " + " WHERE code = ?");
  11.         spQuery.setString(1, sparePart.getPartName());
  12.         spQuery.setString(2, sparePart.getVehicle_type());
  13.         spQuery.setString(3, sparePart.getManufacture());
  14.         spQuery.setBigDecimal(4, sparePart.getPrice());
  15.         spQuery.setInt(5, sparePart.getStockLevel());
  16.         spQuery.setInt(6, sparePart.getThreshold());
  17.         spQuery.setString(7, sparePart.getCode());
  18.  
  19.         conn.commit();
  20.         conn.setAutoCommit(true);
  21.  
  22.         try {
  23.             int i = spQuery.executeUpdate();
  24.  
  25.  
  26.                 if (i != 0) {
  27.                     return "SUCCESS";
  28.                 }
  29.  
  30.         } catch (SQLException e) {
  31.             throw new RuntimeException(e);
  32.         }
  33.         spQuery.close();
  34.         conn.close();
  35.  
  36.         return "Something wrong";
  37.     }
  38.      
  39. public void service(HttpServletRequest request,
  40.         HttpServletResponse response) throws IOException, ServletException {
  41.  
  42.     String code = request.getParameter("spCode");
  43.     String name = request.getParameter("sparePartName");
  44.     String vehicleType = request.getParameter("vehicleType");
  45.     String manufacturer = request.getParameter("manufacturer");
  46.     String p = request.getParameter("price");
  47.     BigDecimal price = new BigDecimal(p.replaceAll("[a-zA-Z,]", "").trim());
  48.     int stock_level = Integer.parseInt(request.getParameter("stkLevel").trim());
  49.     int threshold = Integer.parseInt(request.getParameter("threshold").trim());
  50.  
  51.     SparePart sPart = new SparePart();
  52.  
  53.     sPart.setPartName(name);
  54.     sPart.setVehicle_type(vehicleType);
  55.     sPart.setManufacture(manufacturer);
  56.     sPart.setPrice(price);
  57.     sPart.setStockLevel(stock_level);
  58.     sPart.setThreshold(threshold);
  59.     sPart.setCode(code);
  60.     SparePartDao sd = new SparePartDao();
  61.     String updatedSparePart = null;
  62.     try {
  63.         updatedSparePart = sd.updateSparePart(sPart);
  64.  
  65.     } catch (Exception ex) {
  66.         throw new RuntimeException(ex);
  67.     }
  68.  
  69.    // ServletContext servletContext = getServletContext();
  70.     if (updatedSparePart.equals("SUCCESS")) {
  71.        request.getRequestDispatcher("Navigation.jsp").forward(request, response);
  72.  
  73.     } else {
  74.         request.setAttribute("error",updatedSparePart );
  75.         request.getRequestDispatcher("/createSparePart.jsp").forward(request, response);
  76.     }
  77.  
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top