CH 16会員加入項目2
73239 ワード
3)=>dbに登録ページ作成値を格納
=>>結果
// 회원가입
public boolean insertMember(MemberBean bean) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
boolean flag = false;
try {
con = pool.getConnection();
sql = "insert tblMember(id,pwd,name,gender,"
+ "birthday,email,zipcode,address,hobby,job)"
+ "values(?,?,?,?,?,?,?,?,?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, bean.getId());
pstmt.setString(2, bean.getPwd());
pstmt.setString(3, bean.getName());
pstmt.setString(4, bean.getGender());
pstmt.setString(5, bean.getBirthday());
pstmt.setString(6, bean.getEmail());
pstmt.setString(7, bean.getZipcode());
pstmt.setString(8, bean.getAddress());
///////////////////////////////////////////
String hobby[] = bean.getHobby(); //가입자가 입력한 취미값
//인터넷, 게임, 운동
String lists[] = {"인터넷","여행","게임","영화","운동"};
char hb[] = {'0','0','0','0','0'}; //10101
for(int i=0; i<hobby.length; i++) {
for(int j=0; j<lists.length; j++) {
if(hobby[i].equals(lists[j])) {
hb[j]='1';
break;
}
}
}
pstmt.setString(9, new String(hb));
//////////////////////////////////
pstmt.setString(10, bean.getJob());
//DML이란 Data Manipulation(조작) Language
//insert, update, delete는 실행한 레코드 개수값이 리턴
if(pstmt.executeUpdate()==1) flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
return flag;
}
<%@ page contentType="text/html; charset=EUC-KR"%>
<%request.setCharacterEncoding("EUC-KR");%>
<!--post 방식으로 보낸 data는 반드시 인코딩에 setProperty보다 위에 있어야 함-->
<jsp:useBean id="mgr" class="ch16.MemberMgr"/>
<jsp:useBean id="bean" class="ch16.MemberBean"/>
<jsp:setProperty property="*" name="bean"/>
<%
boolean result = mgr.insertMember(bean);
String msg = "가입 실패";
String url = "member.jsp";
if(result){
msg = "가입 성공";
url = "login.jsp";
}
%>
<script>
alert("<%=msg%>");
location.href="<%=url%>";
</script>
4)ログイン、ログアウト
=>>結果
<!-- login.jsp -->
<%@ page contentType="text/html; charset=EUC-KR" %>
<%
request.setCharacterEncoding("EUC-KR");
String id = (String)session.getAttribute("idKey");
%>
<!DOCTYPE html>
<html>
<head>
<title>로그인</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function loginCheck() {
if (document.loginFrm.id.value == "") {
alert("아이디를 입력해 주세요.");
document.loginFrm.id.focus();
return;
}
if (document.loginFrm.pwd.value == "") {
alert("비밀번호를 입력해 주세요.");
document.loginFrm.pwd.focus();
return;
}
document.loginFrm.submit();
}
</script>
</head>
<body bgcolor="#FFFFCC">
<div align="center">
<br/><br/>
<%if(id!=null){%>
<b><%=id%></b>님 환영합니다.<br/>
제한된 기능을 사용 할 수가 있습니다.<br/>
<a href="logout.jsp">로그아웃</a>
<a href="memberUpdate.jsp">회원수정</a>
<%}else{
id = request.getParameter("id");
%>
<form name="loginFrm" method="post" action="loginProc.jsp">
<table>
<tr>
<td align="center" colspan="2"><h4>로그인</h4></td>
</tr>
<tr>
<td>아 이 디</td>
<td><input name="id" value="<%=(id!=null)?id:"aaa"%>"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pwd" value="1234"></td>
</tr>
<tr>
<td colspan="2">
<div align="right">
<input type="button" value="로그인" onclick="loginCheck()">
<input type="button" value="회원가입" onClick="javascript:location.href='member.jsp'">
</div>
</td>
</tr>
</table>
</form>
<%}%>
</div>
</body>
</html>
<%@ page contentType="text/html; charset=EUC-KR" %>
<jsp:useBean id="mgr" class="ch16.MemberMgr"/>
<%
request.setCharacterEncoding("EUC-KR");
String cPath = request.getContextPath();
String id = request.getParameter("id");
String pass = request.getParameter("pwd");
String msg = "로그인에 실패 하였습니다.";
boolean result = mgr.loginMember(id,pass);
if(result){
session.setAttribute("idKey",id);
msg = "로그인에 성공 하였습니다.";
}
%>
<script>
alert("<%=msg%>");
location.href = "login.jsp";
</script>
// 로그인 : 성공 - true, 실패 - dalse
public boolean loginMember(String id, String pwd) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
boolean flag = false;
try {
con = pool.getConnection();
// select id를 쓰는 경우보다 아래의 경우가 더 효율적으로 id를 검색할 수 있다.
//sql = "select id from tblMember where id=? and pwd=?";
sql = "select count(id) from tblMember where id=? and pwd=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery();
if(rs.next()&&rs.getInt(1)==1)
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return flag;
}
4)ログアウト
<%@ page contentType="text/html; charset=EUC-KR"%>
<%
session.invalidate();
%>
<script>
alert('로그아웃 되었습니다.');
location.href = "login.jsp";
</script>
5)会員情報の修正
<!-- memberUpdate.jsp -->
<%@page import="ch16.MemberBean"%>
<%@page contentType="text/html; charset=EUC-KR"%>
<<jsp:useBean id="mgr" class="ch16.MemberMgr"/>
<%
request.setCharacterEncoding("EUC-KR");
String id = (String)session.getAttribute("idKey");
if(id==null){
response.sendRedirect("login.jsp");
return;
}
MemberBean bean = mgr.getMember(id);
%>
<html>
<head>
<title>회원수정</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
function zipCheck() {
url = "zipSearch.jsp?search=n";
window.open(url, "ZipCodeSearch","width=500,height=300,scrollbars=yes");
}
</script>
</head>
<body bgcolor="#FFFFCC" onLoad="regFrm.id.focus()">
<div align="center">
<br /> <br />
<form name="regFrm" method="post" action="memberUpdateProc.jsp">
<table align="center" cellpadding="5" >
<tr>
<td align="center" valign="middle" bgcolor="#FFFFCC">
<table border="1" cellpadding="2" align="center" width="600">
<tr align="center" bgcolor="#996600">
<td colspan="3"><font color="#FFFFFF"><b>회원 수정</b></font></td>
</tr>
<tr>
<td width="20%">아이디</td>
<td width="80%"><input name="id" size="15"
value="<%=bean.getId() %>" readonly></td>
</tr>
<tr>
<td>패스워드</td>
<td><input type="password" name="pwd" size="15"
value="<%=bean.getPwd()%>"></td>
</tr>
<tr>
<td>이름</td>
<td><input name="name" size="15"
value="<%=bean.getName()%>"></td>
</tr>
<tr>
<td>성별</td>
<td>
<!-- 이렇게도 쓸 수 있음
남<input type="radio" name="gender" value="1"
<%=bean.getGender().equals("1")?"checked":"" %>>
여<input type="radio" name="gender" value="2" <%=bean.getGender().equals("2")?"checked":"" %>>
-->
남<input type="radio" name="gender" value="1">
여<input type="radio" name="gender" value="2">
</td>
<script>
gnd = document.regFrm.gender;
for(i=0; i<gnd.length; i++){
if(gnd[i].value==<%=bean.getGender()%>){
gnd[i].checked = true;
break; // 나머지 라디오 버튼은 볼 필요가 없음
}
}
</script>
</tr>
<tr>
<td>생년월일</td>
<td><input name="birthday" size="6"
value="<%=bean.getBirthday()%>"> ex)830815</td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" size="30"
value="<%=bean.getEmail()%>"></td>
</tr>
<tr>
<td>우편번호</td>
<td><input name="zipcode" size="5"
value="<%=bean.getZipcode()%>" readonly> <input
type="button" value="우편번호찾기" onClick="zipCheck()"></td>
</tr>
<tr>
<td>주소</td>
<td><input name="address" size="45" value="<%=bean.getAddress()%>"></td>
</tr>
<tr>
<td>취미</td>
<td>
<%
String list[] = {"인터넷","여행","게임","영화","운동"};
String hobbys[] = bean.getHobby(); //{"1","0","1","0","1"}
for(int i=0; i<hobbys.length; i++){
%>
<%=list[i] %>
<input type="checkbox" value="<%=list%>" name="hobby"
<%=hobbys[i].equals("1")?"checked":"" %>>
<%
}
%>
</td>
</tr>
<tr>
<td>직업</td>
<td><select name="job">
<option value="0">선택하세요.
<option value="회사원">회사원
<option value="연구전문직">연구전문직
<option value="교수학생">교수학생
<option value="일반자영업">일반자영업
<option value="공무원">공무원
<option value="의료인">의료인
<option value="법조인">법조인
<option value="종교,언론,에술인">종교.언론/예술인
<option value="농,축,수산,광업인">농/축/수산/광업인
<option value="주부">주부
<option value="무직">무직
<option value="기타">기타
</select>
<script>
document.regFrm.job.value="<%=bean.getJob()%>";
</script>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="수정완료">
<input type="reset" value="다시쓰기"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
pub1lic MemberBean getMember(String id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
MemberBean bean = new MemberBean();
try {
con = pool.getConnection();
sql = "select * from tblMember where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next()) {
bean.setId(rs.getString("id"));
bean.setPwd(rs.getString("pwd"));
bean.setName(rs.getString("name"));
bean.setGender(rs.getString("gender"));
bean.setBirthday(rs.getString("birthday"));
bean.setEmail(rs.getString("email"));
bean.setZipcode(rs.getString("zipcode"));
bean.setAddress(rs.getString("address"));
//////////////////////////////////////
//테이블에 저장된 data를 배열로 변환하여 빈즈에 저장
String hobby = rs.getString("hobby");//10101
String hobbys[] = new String[hobby.length()];
for(int i=0;i<hobbys.length;i++) {
hobbys[i]=hobby.substring(i,i+1);
//{"1","0","1","0","1"}
}
bean.setHobby(hobbys);
//////////////////////////////////////
bean.setJob(rs.getString("job"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt, rs);
}
return bean;
}
//회원 수정
public boolean updateMember(MemberBean bean) {
Connection con = null;
PreparedStatement pstmt = null;
String sql = null;
boolean flag = false;
try {
con = pool.getConnection();
sql = "update tblMember set pwd=?,name=?,gender=?,"
+ "birthday=?,email=?,zipcode=?,address=?,hobby=?"
+ ",job=? where id=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, bean.getPwd());
pstmt.setString(2, bean.getName());
pstmt.setString(3, bean.getGender());
pstmt.setString(4, bean.getBirthday());
pstmt.setString(5, bean.getEmail());
pstmt.setString(6, bean.getZipcode());
pstmt.setString(7, bean.getAddress());
String hobby[] = bean.getHobby();
String lists[] = {"인터넷","여행","게임","영화","운동"};
char hb[] = {'0','0','0','0','0'};//10101
for (int i = 0; i < hobby.length; i++) {
for (int j = 0; j < lists.length; j++) {
if(hobby[i].equals(lists[j])) {
hb[j] = '1';
break;
}
}
}
pstmt.setString(8, new String(hb));
pstmt.setString(9, bean.getJob());
pstmt.setString(10, bean.getId());
if(pstmt.executeUpdate()==1) flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con, pstmt);
}
return flag;
}
Reference
この問題について(CH 16会員加入項目2), 我々は、より多くの情報をここで見つけました https://velog.io/@darkoz/CH16-회원가입-프로젝트-hwzliptyテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol