- 회원정보 : 삭제하기
-Controller : Remove
package member.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import member.service.MemberService;
import member.service.MemberServiceImpl;
@WebServlet("/member/remove")
public class Remove extends HttpServlet{
MemberService memberService = MemberServiceImpl.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
memberService.remove(id);
System.out.println(id);
resp.sendRedirect("list");
}
}
- Service : MemberService
package member.service;
import java.util.List;
import member.vo.MemberVo;
public interface MemberService {
public List<MemberVo> list();
public void register(MemberVo memberVo);
public void remove(String id);
}
- Service : MemberServiceImpl
package member.service;
import java.util.List;
import member.dao.MemberDao;
import member.vo.MemberVo;
public class MemberServiceImpl implements MemberService{
private static final MemberService memberService = new MemberServiceImpl();
public static MemberService getInstance() {
return memberService;
}
private MemberServiceImpl() {}
private MemberDao memberDao = MemberDao.getInstance();
// list, register 생략
@Override
public void remove(String id) {
memberDao.remove(id);
}
}
- DAO : MemberDao
package member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import member.vo.MemberVo;
import utils.DBConn;
public class MemberDao {
/**
* singleton
*/
private final static MemberDao dao = new MemberDao();
public static MemberDao getInstance() {
return dao;
}
public MemberDao() {}
/**
*
*/
private Statement stmt;
private PreparedStatement pstmt;
private Connection conn;
// list, register 생략
public void remove(String id) {
try {
conn = DBConn.getConnection();
pstmt = conn.prepareStatement("DELETE T_MEMBER WHERE ID = ?");
int idx = 1;
pstmt.setString(idx++, id);
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- JSP : list(삭제 버튼 추가)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>이름</th>
<th>이메일</th>
<th>가입일</th>
<th>삭제</th>
</tr>
<c:forEach items="${members}" var="member">
<tr>
<td>${member.id}</td>
<td>${member.pwd}</td>
<td>${member.name}</td>
<td>${member.email}</td>
<td>${member.joinDate}</td>
<td><a href="remove?id=${member.id}">삭제</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>