[JSP] 회원가입 / 회원인증 - MemberMgr 전체 코드


pack_Member/MemberMgr.java


package pack_Member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class MemberMgr {

	private DBConnectionMgr pool;
	
	Connection 			conn 	= null;
	PreparedStatement 	pstmt 	= null;
	ResultSet 			rs 		= null;
	String 				sql 	= null;

	public MemberMgr() {
		try {
			pool = DBConnectionMgr.getInstance();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	//// 아이디 중복확인  ////
	public boolean checkId(String uId) {
		
		boolean flag = false;
		
		try {
			
			conn = pool.getConnection();
			sql = "select uId from tblMember where uId=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, uId);
			
			// 데이터가 존재한다면 flag가 true로 바뀜
			flag = pstmt.executeQuery().next();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt, rs);
		}
		
		return flag;
	}
	
	//// 우편번호 검색 ////
	public Vector<ZipCodeBean> zipCodeRead(String area3){
		
		Vector<ZipCodeBean> vlist = new Vector<ZipCodeBean>();
		
		try {
			conn = pool.getConnection();
			sql = "select * from tblZipcode where area3 like ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "%" + area3 + "%");
			rs = pstmt.executeQuery();
			
			if (rs != null) {
				while (rs.next()) { 
					ZipCodeBean zipBean = new ZipCodeBean();
					zipBean.setZipCode(rs.getString("zipCode"));
					/*
					 * = zipBean.setuZip(rs.getString(1));
					 */					
					zipBean.setArea1(rs.getString("area1"));
					/*
					 * =  zipBean.setArea1(rs.getString(2));
					 */					
					zipBean.setArea2(rs.getString("area2"));
					zipBean.setArea3(rs.getString("area3"));
					zipBean.setArea4(rs.getString("area4"));
					
					vlist.add(zipBean);
				}				
			}
			
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt, rs);
		}
		return vlist;
	}
	
	//// 회원가입 ////
	public boolean insertMember(MemberBean bean) {
		
		boolean flag = false;
		
		try {
			conn = pool.getConnection();
			
			sql = "insert into tblMember(uId, uPw, uName, uGender, uBirthday, uEmail, uZip, uAddr, uHobby, uJob)"
					+ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, bean.getuId());
			pstmt.setString(2, bean.getuPw());
			pstmt.setString(3, bean.getuName());
			pstmt.setString(4, bean.getuGender());
			pstmt.setString(5, bean.getuBirthday());
			pstmt.setString(6, bean.getuEmail());
			pstmt.setString(7, bean.getuZip());
			pstmt.setString(8, bean.getuAddr());
			
			String[] hobby = bean.getuHobby();
			String[] strList = {"코딩", "여행", "게임", "영화", "운동"};
			char[] hobbyList = {'0', '0', '0', '0', '0'};
			
			for (int i = 0; i < hobby.length; i++) {
				// form에서 선택한 갯수
				for (int j = 0; j < strList.length; j++) {
					if (hobby[i].equals(strList[j])) {
						hobbyList[j] = '1';
					}
				}
			}
			
			pstmt.setString(9, new String(hobbyList));
			pstmt.setString(10, bean.getuJob());
			
			int cnt = pstmt.executeUpdate();
			if (cnt == 1) { flag = true; }
			// cnt > 0
			
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt);
		}
		return flag;
	}
	
	//// 로그인 ////
	public boolean loginMember(String uId, String uPw) {
		
		boolean flag = false;
		
		try {
			conn = pool.getConnection();
			sql = "select uId from tblMember where uId=? and uPw=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, uId);
			pstmt.setString(2, uPw);
			rs = pstmt.executeQuery();
			flag = rs.next();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return flag;
	}
	
	//// 회원 정보 수정 입력 폼 ////
	public Vector modifyMember(String uId) {
		
		Vector<MemberBean> vlist = new Vector<>();
				
		try {
			conn = pool.getConnection();
			sql = "select * from tblMember where uId=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, uId);

			rs = pstmt.executeQuery();
			
			if (rs != null) {
				while (rs.next()) { 
					MemberBean memberBean = new MemberBean();
					memberBean.setuId(rs.getString("uId"));			
					memberBean.setuPw(rs.getString("uPw"));			
					memberBean.setuName(rs.getString("uName"));			
					memberBean.setuEmail(rs.getString("uEmail"));			
					memberBean.setuGender(rs.getString("uGender"));			
					memberBean.setuBirthday(rs.getString("uBirthday"));			
					memberBean.setuZip(rs.getString("uZip"));			
					memberBean.setuAddr(rs.getString("uAddr"));		
					
					// DB에서 반환받은 셀의 데이터 String[] 자료형으로 변경 후 필드에 대입
					String str = rs.getString("uHobby");
					String[] hobbyList = {"0", "0", "0", "0", "0"};
					char ch = '0';
					for (int i = 0; i < str.length(); i++) {
						ch = str.charAt(i);
						hobbyList[i] = Character.toString(ch);
					}
					memberBean.setuHobby(hobbyList);		
					// uHobby
					
					memberBean.setuJob(rs.getString("uJob"));		

					vlist.add(memberBean);
				}				
			}
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt, rs);
		}
		return vlist;
		
	}
	
	//// 회원 정보 수정 ////
	public boolean modifyMember(String uPw, String uEmail, String uJob, String uId) {
		
		boolean flag = false;
				
		try {
			conn = pool.getConnection();
			sql = "update tblMember set uPw=?, uEmail=?, uJob=? where uId=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, uPw);
			pstmt.setString(2, uEmail);
			pstmt.setString(3, uJob);
			pstmt.setString(4, uId);

			 int cnt = pstmt.executeUpdate();
			
			if (cnt > 0) { flag = true; }
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt);
		}
		return flag;
	}
	
	//// 회원 탈퇴 ////
	public boolean deleteMember(String uId) {
		
		boolean flag = false;
		
		try {
			conn = pool.getConnection();
			
			sql = "delete from tblMember where uId = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, uId);
			
			int cnt = pstmt.executeUpdate();
			if (cnt > 0)  flag = true;
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt);
		}
		return flag;
	}
}

Categories:

JSP/Servlet