OracleDB를 사용
SqlDeveloper로 memberT테이블을 만들어 놓고 아래 코드 작성
MemberBean.java
package magic.member;
import java.sql.Timestamp;
public class MemberBean {
private String mem_uid;
private String mem_pwd;
private String pwd_check;
private String mem_name;
private String mem_email;
private Timestamp mem_regdate;
private String mem_addr;
public String getPwd_check() {
return pwd_check;
}
public void setPwd_check(String pwd_check) {
this.pwd_check = pwd_check;
}
public String getMem_uid() {
return mem_uid;
}
public void setMem_uid(String mem_uid) {
this.mem_uid = mem_uid;
}
public String getMem_pwd() {
return mem_pwd;
}
public void setMem_pwd(String mem_pwd) {
this.mem_pwd = mem_pwd;
}
public String getMem_name() {
return mem_name;
}
public void setMem_name(String mem_name) {
this.mem_name = mem_name;
}
public String getMem_email() {
return mem_email;
}
public void setMem_email(String mem_email) {
this.mem_email = mem_email;
}
public Timestamp getMem_regdate() {
return mem_regdate;
}
public void setMem_regdate(Timestamp mem_regdate) {
this.mem_regdate = mem_regdate;
}
public String getMem_addr() {
return mem_addr;
}
public void setMem_addr(String mem_addr) {
this.mem_addr = mem_addr;
}
}
MemberBeanDB.java
package magic.member;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import myUtil.HanConv;
public class MemberDBBean {
private static MemberDBBean instance = new MemberDBBean();
// 참조변수 instance를 객체로 받아서 사용함
// 싱글턴패턴
public static MemberDBBean getInstance() {
return instance;
}
public Connection getConnection() throws Exception {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle");
return ds.getConnection(); // 재사용을 위해 만들어 놓음
}
// insert쿼리 실행 데이터를집어넣음
public int insertMember(MemberBean member) throws Exception {
String insertQuery = "insert into memberT values(?,?,?,?,?,?)";
int re = -1;
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement(insertQuery);
ps.setString(1, member.getMem_uid());
ps.setString(2, member.getMem_pwd());
ps.setString(3, HanConv.toKor(member.getMem_name()));
ps.setString(4, member.getMem_email());
ps.setTimestamp(5, member.getMem_regdate());
ps.setString(6, HanConv.toKor(member.getMem_addr()));
ps.executeUpdate();
re = 1;
ps.close();
con.close();
System.out.println("추가성공");
} catch (Exception e) {
e.printStackTrace();
System.out.println("추가실패");
}
return re;
}
// ID 중복확인
public int ConfirmID(String id) throws Exception {
int re = -1;
String selectQuery = "select mem_uid from memberT where mem_uid=?";
try {
Connection con = getConnection(); // 컨넥션 정보를 받음
PreparedStatement ps = con.prepareStatement(selectQuery);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
re = 1; // 중복 될 경우
} else {
re = -1; // 중복 안 될 경우
}
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return re;
}
// 로그인 할때 아이디가 존재하는지 안하는지 확인 여부 메소드
public int userCheck(String id, String pwd) throws Exception {
int re = -1;
String sql = "select mem_pwd from memberT where mem_uid=?";
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) { // 아이디가 일치하는 로우가 존재
String db_mem_pwd = rs.getString("mem_pwd");
if (db_mem_pwd.equals(pwd)) { // 비밀번호 일치
re = 1;
} else { // 비밀번호 불일치
re = 0;
}
} else { // 아이디가 존재하지 않음
re = -1;
}
con.close();
ps.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return re;
}
// 아이디가 일치하는 멤버의 정보를 얻어오는 메소드
public MemberBean getMember(String id) throws Exception {
String sql = "select * from memberT where mem_uid=?";
MemberBean member = null;
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
member = new MemberBean();
member.setMem_uid(rs.getString("mem_uid"));
member.setMem_pwd(rs.getString("mem_pwd"));
member.setMem_name(rs.getString("mem_name"));
member.setMem_email(rs.getString("mem_email"));
member.setMem_regdate(rs.getTimestamp("mem_regdate"));
member.setMem_addr(rs.getString("mem_addr"));
}
con.close();
ps.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
return member;
}
// 회원정보 수정 메서드
public int updateMember(MemberBean member) throws Exception {
Connection con = null;
PreparedStatement pstmt = null;
String sql = "update memberT set mem_pwd=?, mem_email=?, mem_addr=?";
sql += "where mem_uid=?";
int re = -1;
try {
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, member.getMem_pwd());
pstmt.setString(2, member.getMem_email());
pstmt.setString(3, HanConv.toKor(member.getMem_addr()));
pstmt.setString(4, member.getMem_uid());
re = pstmt.executeUpdate();
} catch (Exception e) {
System.out.println(e.getMessage());
}
pstmt.close();
con.close();
return re;
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center">
<form action="loginOK.jsp" method="post">
<tr>
<td align="center" >사용자 ID</td>
<td>
<input type="text" name="mem_uid">
</td>
</tr>
<tr>
<td align="center">비밀번호</td>
<td>
<input type="text" name="mem_pwd">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="로그인" >
<input type="button" value="회원가입" onclick="javascript:window.location='register.jsp'">
</td>
</tr>
</form>
</table>
</body>
</html>
loginOK.jsp
<%@page import="magic.member.MemberDBBean"%>
<%@page import="magic.member.MemberBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("mem_uid");
String pwd = request.getParameter("mem_pwd");
MemberDBBean manager = MemberDBBean.getInstance();
int check = manager.userCheck(id, pwd);
MemberBean mb = manager.getMember(id);
if(mb == null) {
out.print("<script>alert('존재하지 않는 회원');</script>");
} else {
String name = mb.getMem_name();
if(check == 1) {
session.setAttribute("uid", id);
session.setAttribute("name", name);
session.setAttribute("Member", "yes"); // 임의로 회원일 경우 yes값을 준다
response.sendRedirect("main.jsp");
} else if(check == 0) {
out.print("<script>alert('비밀번호가 틀렸습니다.');</script>");
out.print("<script>history.go(-1);</script>");
} /* else { //실제로 검사는 안하지만 case로 처리함
out.print("<script>alert('아이디가 맞지 않습니다');</script>");
out.print("<script>history.go(-1);</script>");
} */
}
%>
logOut.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
session.invalidate();
response.sendRedirect("login.jsp");
%>
register.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript" src="script.js" charset="utf-8"></script>
</head>
<body>
<table border="1">
<form name="reg_frm" method="post" action="registerOk.jsp">
<tr>
<td colspan="2" align="center">
<h1>회원 가입 신청</h1>
'*' 표시 항목은 필수 입력 항목입니다.
</td>
</tr>
<tr>
<td width="80">User ID</td>
<td>
<input name="mem_uid" size="20" maxlength="15">*
</td>
</tr>
<tr>
<td width="80">암호</td>
<td>
<input name="mem_pwd" type="password" size="20">*
</td>
</tr>
<tr>
<td width="80">암호 확인</td>
<td>
<input name="pwd_check" type="password" size="20">*
</td>
</tr>
<tr>
<td width="80">이 름</td>
<td>
<input name="mem_name" size="20">*
</td>
</tr>
<tr>
<td width="80">E-mail</td>
<td>
<input name="mem_email" size="30">*
</td>
</tr>
<tr>
<td width="80">주 소</td>
<td>
<input name="mem_addr" size="40">*
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="등록" onclick="check_ok()" onkeyup="len_chk()">
<input type="reset" value="다시입력">
<input type="button" value="가입안함" onclick="javascript:window.location='login.jsp'">
</td>
</tr>
</form>
</table>
</body>
</html>
registerOk.jsp
<%@page import="magic.member.MemberDBBean"%>
<%@page import="magic.member.MemberBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("mem_uid");
String pwd = request.getParameter("mem_pwd");
MemberDBBean manager = MemberDBBean.getInstance();
int check = manager.userCheck(id, pwd);
MemberBean mb = manager.getMember(id);
if(mb == null) {
out.print("<script>alert('존재하지 않는 회원');</script>");
} else {
String name = mb.getMem_name();
if(check == 1) {
session.setAttribute("uid", id);
session.setAttribute("name", name);
session.setAttribute("Member", "yes"); // 임의로 회원일 경우 yes값을 준다
response.sendRedirect("main.jsp");
} else if(check == 0) {
out.print("<script>alert('비밀번호가 틀렸습니다.');</script>");
out.print("<script>history.go(-1);</script>");
} /* else { //실제로 검사는 안하지만 case로 처리함
out.print("<script>alert('아이디가 맞지 않습니다');</script>");
out.print("<script>history.go(-1);</script>");
} */
}
%>
main.jsp(로그인 했을 때 뜨는 화면)
<%@page import="myUtil.HanConv"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
if(session.getAttribute("Member") == null) {
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
%>
<%
String uid = (String)session.getAttribute("uid");
String name = (String)session.getAttribute("name");
%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center">
<form action="logOut.jsp" method="post">
<tr>
<td>
안녕하세요. <%= name%> (<%= uid %>)님
</td>
</tr>
<tr>
<td>
<input type="submit" value="로그아웃">
<input type="button" value="회원정보 변경" onclick="javascript:window.location='memberUpdate.jsp'">
</td>
</tr>
</form>
</table>
</body>
</html>
memberUpdate.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String uid = (String)session.getAttribute("uid");
String name = (String)session.getAttribute("name");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript" src="script.js" charset="utf-8"></script>
</head>
<body>
<table border="1">
<form name="update_form" method="post" action="memberUpdateOk.jsp">
<tr>
<td colspan="2" align="center">
<h1>회원 정보 수정</h1>
'*' 표시 항목은 필수 입력 항목입니다.
</td>
</tr>
<tr>
<td width="80">User ID</td>
<td>
<%= uid %>
</td>
</tr>
<tr>
<td width="80">암호</td>
<td>
<input name="mem_pwd" type="password" size="20">*
</td>
</tr>
<tr>
<td width="80">암호 확인</td>
<td>
<input name="pwd_check" type="password" size="20">*
</td>
</tr>
<tr>
<td width="80">이 름</td>
<td>
<%= name %>
</td>
</tr>
<tr>
<td width="80">E-mail</td>
<td>
<input name="mem_email" size="30">*
</td>
</tr>
<tr>
<td width="80">주 소</td>
<td>
<input name="mem_addr" size="40">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" value="수정" onclick="update_check_ok()">
<input type="reset" value="다시입력">
<input type="button" value="수정안함" onclick="javascript:window.location='main.jsp'">
</td>
</tr>
</form>
</table>
</body>
</html>
memberUpdateOk.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="magic.member.*"%>
<jsp:useBean id="mb" class="magic.member.MemberBean"></jsp:useBean>
<jsp:setProperty property="*" name="mb"/>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<%
String uid = (String)session.getAttribute("uid");
mb.setMem_uid(uid);
MemberDBBean manager = MemberDBBean.getInstance();
int re = manager.updateMember(mb);
if (re == 1) {
%>
<script>
alert("입력하신대로 회원 정보가 수정되었습니다.");
<%
session.invalidate();
%>
document.location.href = 'login.jsp';
</script>
<%
} else {
// 수정이 실패 되는 경우는 여러대에서 해당 테이블에 접속할때 오류가 나는 여러가지 경우에 발생한다.
%>
<script>
alert("수정이 실패되었습니다..");
history.go(-1);
</script>
<%
}
%>
</head>
<body>
</body>
</html>
script.js
function check_ok(){
//if(document.reg_frm.mem_uid.value == ""){}
if(document.reg_frm.mem_uid.value.length == 0){
alert("아이디를 써주세요.");
reg_frm.mem_uid.focus();
}
else if(document.reg_frm.mem_uid.value.length < 4){
alert("아이디는 4글자 이상이어야 합니다.");
reg_frm.mem_uid.focus();
}
else if(document.reg_frm.mem_pwd.value.length == 0){
alert("패스워드는 반드시 입력해야 합니다.");
reg_frm.mem_pwd.focus();
}
else if(document.reg_frm.mem_pwd.value != document.reg_frm.pwd_check.value){
alert("패스워드가 일치하지 않습니다.");
reg_frm.pwd_check.focus();
}
else if(document.reg_frm.mem_name.value.length == 0){
alert("이름을 써주세요.");
reg_frm.mem_name.focus();
}
else if(document.reg_frm.mem_email.value.length == 0){
alert("Email을 써주세요.");
reg_frm.mem_email.focus();
}
document.reg_frm.submit();
}
function update_check_ok() {
if(document.update_form.mem_pwd.value.length == 0){
alert("패스워드는 반드시 입력해야 합니다.");
update_form.mem_pwd.focus();
return;
}
if(document.update_form.mem_pwd.value != document.update_form.pwd_check.value){
alert("패스워드가 일치하지 않습니다.");
update_form.pwd_check.focus();
return;
}
if(document.update_form.mem_email.value.length == 0){
alert("Email을 써주세요.");
update_form.mem_email.focus();
return;
}
document.update_form.submit();
}
모르는게 있으시면 질문남겨놓으시면 답변해드리겠습니다^^
'개발 > JSP' 카테고리의 다른 글
[jsp] 게시판 만들기 (0) | 2021.04.19 |
---|---|
[JSP]DB커넥션 풀(DBCP) (0) | 2021.04.05 |
[JSP/JDBC]Oracle과 이클립스 JSP 연동/ 테이블 조회 (0) | 2021.04.03 |
[JSP] 액션 태그를 활용하여 사각형 면적 구하기 (0) | 2021.03.26 |
[JSP] session의 사용 (0) | 2021.03.24 |