[JSP] 회원가입 / 회원인증 ⑥ - 회원 정보 수정
20 Dec 2021 -
4 minute read
member/MemberInfo_Mod.jsp
<%@page import="pack_Member.MemberBean"%>
<%@page import="java.util.Vector"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="memberMgr" class="pack_Member.MemberMgr" scope="page" />
<%
String sessionUId = (String)session.getAttribute("idKey");
Vector<MemberBean> vlist = memberMgr.modifyMember(sessionUId);
%>
<% if (sessionUId != null) {
MemberBean memberBean = (MemberBean)vlist.get(0);
String uId = memberBean.getuId();
String uPw = memberBean.getuPw();
String uName = memberBean.getuName();
String uEmail = memberBean.getuEmail();
String uGender = memberBean.getuGender();
String uBirthday = memberBean.getuBirthday();
String uZip = memberBean.getuZip();
String uAddr = memberBean.getuAddr();
String[] uHobby = memberBean.getuHobby();
String uJob = memberBean.getuJob();
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>회원 정보 수정</title>
<link rel="stylesheet" href="">
</head>
<body>
<div id="wrap">
<form action="Member_ModProc.jsp" name="modFrm" id="modFrm" method="post">
<table>
<tbody>
<caption>회원정보 수정</caption>
<tr>
<th class="req">아이디</th>
<td><%=uId %></td>
</tr>
<tr>
<th class="req">비밀번호</th>
<td>
<input type="password" name="uPw" id="uPw" value="<%=uPw %>" %>
</td>
</tr>
<tr>
<th>비밀번호 확인</th>
<td>
<input type="password" id="uPw_Re">
</td>
</tr>
<tr>
<th class="req">이름</th>
<td><%=uName %></td>
</tr>
<tr>
<th class="req">이메일</th>
<td>
<input type="text" name="uEmail" id="uEmail" value="<%=uEmail%>">
</td>
</tr>
<tr>
<th>우편번호</th>
<td><%=uZip %></td>
</tr>
<tr>
<th>주소</th>
<td><%=uAddr %></td>
</tr>
<tr>
<th>성별</th>
<td>
<label>
남<input type="radio" name="uGender" value="1" disabled
<% if (uGender.equals("1")) out.print("checked"); %>>
</label>
<label>
여<input type="radio" name="uGender" value="0" disabled
<% if (uGender.equals("0")) out.print("checked"); %>>
</label>
</td>
</tr>
<tr>
<th>생년월일</th>
<td><%=uBirthday%></td>
</tr>
<tr>
<th>취미</th>
<td>
코딩<label><input type="checkbox" name="uHobby" value="코딩" disabled
<% if (uHobby[0].equals("1")) out.print("checked"); %>></label>
여행<label><input type="checkbox" name="uHobby" value="여행" disabled
<% if (uHobby[1].equals("1")) out.print("checked"); %>></label>
게임<label><input type="checkbox" name="uHobby" value="게임" disabled
<% if (uHobby[2].equals("1")) out.print("checked"); %>></label>
영화<label><input type="checkbox" name="uHobby" value="영화" disabled
<% if (uHobby[3].equals("1")) out.print("checked"); %>></label>
운동<label><input type="checkbox" name="uHobby" value="운동" disabled
<% if (uHobby[4].equals("1")) out.print("checked"); %>></label>
</td>
</tr>
<tr>
<th>직업</th>
<td>
<select name="uJob">
<option value="0" <% if(uJob.equals("0")) out.print("selected"); %>>-선택-</option>
<option value="회사원" <% if(uJob.equals("회사원")) out.print("selected"); %>>회사원</option>
<option value="학생" <% if(uJob.equals("학생")) out.print("selected"); %>>학생</option>
<option value="공무원" <% if(uJob.equals("공무원")) out.print("selected"); %>>공무원</option>
<option value="연구전문직" <% if(uJob.equals("연구전문직")) out.print("selected"); %>>연구전문직</option>
<option value="일반자영업" <% if(uJob.equals("일반자영업")) out.print("selected"); %>>일반자영업</option>
<option value="의료인" <% if(uJob.equals("의료인")) out.print("selected"); %>>의료인</option>
<option value="언론/예술인" <% if(uJob.equals("언론/예술인")) out.print("selected"); %>>언론/예술인</option>
<option value="무직" <% if(uJob.equals("무직")) out.print("selected"); %>>무직</option>
<option value="기타"<% if(uJob.equals("기타")) out.print("selected"); %>>기타</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" id="modBtn">정보수정</button>
<button type="reset">다시입력</button>
<button type="button" id="mainBtn">메인으로</button>
</td>
</tr>
</tbody>
</table>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</body>
</html>
<% } else { %>
<script>
alert("비정상적인 접속입니다. \n 메인페이지로 이동합니다.")
/* 로그아웃(세션 소멸) 후 새로고침 등을 통해 페이지를 보려고 할 때 */
location.href="../Index.jsp"
</script>
<% } %>
<script>
$(function(){
$("#modBtn").click(function(){
let uPw = $("#uPw").val();
let uPw_Re = $("#uPw_Re").val();
let uEmail = $("#uEmail").val();
if (uPw == "") {
alert("수정할 비밀번호를 입력하세요.");
$("#uPw").focus();
} else if (uPw != uPw_Re) {
alert("비밀번호가 일치하지 않습니다.");
$("#uPw_Re").val("");
$("#uPw_Re").focus();
} else if (uEmail == "") {
alert("수정할 이메일을 입력하세요.");
$("#uEmail").focus();
} else {
$("#modFrm").submit();
}
});
$("#mainBtn").click(function(){
location.href="../Index.jsp"
});
});
</script>
member/Member_ModProc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String uPw = request.getParameter("uPw");
String uEmail = request.getParameter("uEmail");
String uJob = request.getParameter("uJob");
%>
<jsp:useBean id="memberMgr" class="pack_Member.MemberMgr" scope="page" />
<%
String sessionUId = (String)session.getAttribute("idKey");
boolean res = memberMgr.modifyMember(uPw, uEmail, uJob, sessionUId);
%>
<script>
<% if (res) { %>
alert("회원 정보 수정이 완료되었습니다. 메인페이지로 이동합니다.");
location.href = "../Index.jsp";
<% } else { %>
let msg = "오류가 발생했습니다. 다시 시도해주세요. \n"
msg += "오류가 계속되면 관리자에게 연락해주세요."
alert(msg);
history.back();
<% } %>
</script>
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 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;
}
}