[JSP] 회원가입 / 회원인증 ④ - 회원가입
18 Dec 2021 -
2 minute read
pack_Member/MemberBean.java
package pack_Member;
public class MemberBean {
private String uId;
private String uPw;
private String uName;
private String uGender;
private String uBirthday;
private String uEmail;
private String uZip;
private String uAddr;
private String[] uHobby;
private String uJob;
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public String getuPw() {
return uPw;
}
public void setuPw(String uPw) {
this.uPw = uPw;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuGender() {
return uGender;
}
public void setuGender(String uGender) {
this.uGender = uGender;
}
public String getuBirthday() {
return uBirthday;
}
public void setuBirthday(String uBirthday) {
this.uBirthday = uBirthday;
}
public String getuEmail() {
return uEmail;
}
public void setuEmail(String uEmail) {
this.uEmail = uEmail;
}
public String getuZip() {
return uZip;
}
public void setuZip(String uZip) {
this.uZip = uZip;
}
public String getuAddr() {
return uAddr;
}
public void setuAddr(String uAddr) {
this.uAddr = uAddr;
}
public String[] getuHobby() {
return uHobby;
}
public void setuHobby(String[] uHobby) {
this.uHobby = uHobby;
}
public String getuJob() {
return uJob;
}
public void setuJob(String uJob) {
this.uJob = uJob;
}
}
pack_Member/MemberMgr.java
package pack_Member;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 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;
}
}
member/MemberProc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="memberMgr" class="pack_Member.MemberMgr" scope="page" />
<!-- MemberBean 클래스 객체에 Setter를 사용하여 필드에 데이터 초기화 -->
<jsp:useBean id="bean" class="pack_Member.MemberBean" scope="page" />
<jsp:setProperty name="bean" property="*" />
<%
boolean res = memberMgr.insertMember(bean);
%>
<script>
<% if (res) { %>
alert("회원가입이 완료되었습니다. 로그인 페이지로 이동합니다.");
location.href = "Login.jsp";
<% } else { %>
let msg = "회원가입중에 오류가 발생했습니다. 다시 시도해주세요."
alert(msg);
history.back();
<% } %>
</script>