自分用 研修 削除 mvc

コントローラー

	   /**
		 * キャンセルする注文番号に該当する注文のリストを取得する
		 * @param request　リクエストオブジェクト
		 * @return 次画面のURL
		 */
	public String cancelEntry(HttpServletRequest req){	
		List<String> msgList = new ArrayList<String>();
		String nextPath = "/systemError";
		
		try{
			List<LogOrdersDTO> list = (List<LogOrdersDTO>) req.getSession(false).getAttribute("log");
			
			LogOrdersDTO formdto = null;
			int cancelNo = Integer.parseInt(req.getParameter("no"));
			for (LogOrdersDTO element : list){
				if (element.getNo() == cancelNo){
					formdto = element;
				}
			}
			HttpSession session = req.getSession(false);
			session.removeAttribute("LogOrders");
			session.setAttribute("LogOrders", formdto);
			
			//注文日と今日の日付を比較
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String sdfOrderDate = sdf.format(formdto.getOrderDate());
			java.util.Calendar cal = java.util.Calendar.getInstance();
			int y = cal.get(java.util.Calendar.YEAR);
			int m = cal.get(java.util.Calendar.MONTH) + 1;
			int d = cal.get(java.util.Calendar.DATE);
			String preToday = y + "-" + m + "-" + d;
			Date test = sdf.parse(preToday);
			String today = sdf.format(test);
			boolean isOrderDay = sdfOrderDate.equals(today);
			boolean isShipping = formdto.getShipping().equals("未出荷");
			boolean isCancel = isOrderDay && isShipping;
			req.setAttribute("isCancel", isCancel);
			
			nextPath = "/orderCancelEntry";
		}catch(Exception e){
			
			msgList.add("キャンセル商品情報の取得に失敗しました。");
			req.setAttribute("message", msgList);
			nextPath = "/systemError";
			e.printStackTrace();
		}
		
		return nextPath;
	}
	
	/**
	 * セッションから画面DTOを取得し、ConnectionとLogOrdersDTOを引き渡す。
	 * @param request　リクエストオブジェクト
	 * @return Stirng 次ページのURL
	 * @throws SQLException DAOで発生する例外
	 */
	public String cancel(HttpServletRequest req){
		List<String> msgList = new ArrayList<String>();
		String nextPath = "/systemError";
		Connection conn = null;
		
		try{
			conn = getConnection();
			conn.setAutoCommit(false);
			HttpSession session = req.getSession(false);
			LogOrdersDTO formDto = (LogOrdersDTO) session.getAttribute("LogOrders");
			
			OrderService service = new OrderService();
			service.cancel(conn, formDto);
			
			nextPath = "/orderCancelDone";
			conn.commit();
		}catch(Exception e){
			if(conn != null){
				try{
					conn.rollback();
				}catch(SQLException e1){
					e1.printStackTrace();
				}
			}
			msgList.add("注文のキャンセルに失敗しました。");
			req.setAttribute("message", msgList);
			nextPath = "/systemError";
			e.printStackTrace();
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException e){
					e.printStackTrace();
				}
			}
		}
		
		return nextPath;
		
	}

}



アクション

/**
	 * キャンセル処理（deleteとupdate）を実行する
	 * @param conn Connectionオブジェクト, formDto LogOrdersの画面DTO
	 * @return void
	 * @throws SQLException
	 */
	public void cancel(Connection conn, LogOrdersDTO formDto) throws SQLException{
		LogDTO dto = new LogDTO();
		dto.setNo(formDto.getNo());
		dto.setOrderDate(formDto.getOrderDate());
		dto.setOrderQuantity(formDto.getOrderQuantity());
		dto.setShippingDate(formDto.getShippingDate());
		dto.setName(formDto.getName());
		dto.setId(formDto.getId());
		
		OrdersDAO ordersDao = new OrdersDAO(conn); 
		int resultDelete = ordersDao.orderDelete(dto);
		if(resultDelete == 1){
			System.out.println("注文が削除されました");
		}else{
			System.out.println("注文が削除の削除に失敗しました" + resultDelete);
			System.out.println("resultDelete:" + resultDelete);
		}
		
		ItemsDAO itemsDao = new ItemsDAO(conn);
		int resultUpdate = itemsDao.reservedUpdate(dto);
		if(resultUpdate == 1){
			System.out.println("在庫引当数を書き換えました");
		}else{
			System.out.println("在庫引当数の書き換えに失敗しました" + resultUpdate);
			System.out.println("resultDelete:" + resultUpdate);
		}
		
	}
}


DAO

public class OrdersDAO {
	private final String SEQSQL = "SELECT NEXTVAL('orderno')";
	private final String ORDERINSQL = "INSERT INTO orders(no,item_id,order_quantity,shipping_quantity,user_id,order_date,shipping_date) VALUES(?,?,?,?,?,?,?)";
	private final String SELECTLOG="select no,order_date,name,order_quantity,shipping_date,id from orders,items where orders.item_id=items.id and user_id=? ORDER BY no ASC";
	private final String SELECTLOG2="select no,order_date,name,order_quantity,shipping_date,id from orders,items where orders.item_id=items.id ORDER BY no ASC";
	private final String ORDERDELETE = "DELETE FROM orders WHERE no = ?";
	
	private Connection conn;
	
	public OrdersDAO(Connection conn){
		this.conn=conn;
	}

}
	
	/**
	 * 注文削除
	 * @param dto LogテーブルDTO
	 * @return データ削除行数
	 * @throws SQLException
	 */
	public int orderDelete(LogDTO dto) throws SQLException {
		PreparedStatement pstmt = this.conn.prepareStatement(ORDERDELETE);
		pstmt.setInt(1, dto.getNo());
		return pstmt.executeUpdate();
	}
}