berryberrybin / kosta-jsp

jsp study
0 stars 0 forks source link

DB사용 연습 - 회원정보 : member 추가 #23

Open berryberrybin opened 2 years ago

berryberrybin commented 2 years ago

image

berryberrybin commented 2 years ago

memberForm.html

image

berryberrybin commented 2 years ago

AddMemberServlet.java

@WebServlet("/addMember")
public class AddMemberServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String url = "error.jsp";
        String errMsg = "등록 실패하였습니다.";
        boolean isRedirect = false; // 이동방식을 결정하는 상태변수 (true이면 redirect방식으로 이동, false이면 forward방식으로 이동)

        // 전송된 데이터 받기
        String id = request.getParameter("id");
        String pwd = request.getParameter("pwd");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String phone = request.getParameter("phone");
        String addr = request.getParameter("addr");

        // 전송된 데이터에 대한 유효성 체크 필요
        if (id == null || id.equals("") || pwd == null || pwd.equals("") || name == null || name.equals("")
                || age == null || age.equals("") || phone == null || phone.equals("") || addr == null
                || addr.equals("")) {
            // 오류이면 메시지 출력
            errMsg = "입력값이 충분하지 않습니다. 다시 확인해주세요.";
        } else {
            // id 중복 체크
            MemberDAO dao = new MemberDAOImpl();
            if (dao.duplicateById(id)) {
                errMsg = id + "이미 사용중입니다. ";

            } else {
                Member member = new Member(id, pwd, name, Integer.parseInt(age), phone, addr, null);
                // 등록완료 되었을 경우
                if (dao.insert(member) > 0) {
                    url = "selectAll";// 서블릿 이동
                    isRedirect = true;
                }
            }
        }

        if (isRedirect) {
            response.sendRedirect(url);
        } else {
            request.setAttribute("error", errMsg);

            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }
}
berryberrybin commented 2 years ago

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
    alert("${error}");
    history.back();
</script>
</body>
</html>
berryberrybin commented 2 years ago

MemberDAOImpl.java

insert - member 추가

    @Override
    public int insert(Member member) {
        Connection con = null;
        PreparedStatement ps = null;
        int result = 0; // 0 이면 실패 1이면 성공 
        String sql = "insert into member(id,pwd,name,age,phone,addr, join_date) values(?,?,?,?,?,?, sysdate)";

        try {
            con = DbUtil.getConnection();
            ps = con.prepareStatement(sql);

            ps.setString(1, member.getId());
            ps.setString(2, member.getPwd());
            ps.setString(3, member.getName());
            ps.setInt(4, member.getAge());
            ps.setString(5, member.getPhone());
            ps.setString(6, member.getAddr());

            result = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtil.dbClose(ps, con);
        }
        return result;
    }

duplicateById - member의 id 중복 체크

    @Override
    public boolean duplicateById(String id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT ID FROM MEMBER WHERE ID=?";

        try {
            con = DbUtil.getConnection();
            ps = con.prepareStatement(sql);
            ps.setString(1, id);

            rs = ps.executeQuery();
            return rs.next();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtil.dbClose(rs, ps, con);
        }
        return false;
    }