Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 이클립스
- 예외처리
- 에러페이지
- 어노테이션
- 오버로딩
- 메소드
- 메서드
- 클래스
- AWT
- layout
- struts2
- Spring
- 전화걸기
- Eclips
- OGNL
- paint
- 국제화
- mybatis
- 배열
- JavaScript
- 생성자
- 안드로이드
- JSP
- Android
- Java
- oracle
- 기본
- Menu
- Graphic
- HTML
Archives
- Today
- Total
note
JSP 회원 관리 프로그램 본문
1.테이블 생성
2.자바빈 구성
3.DAO 생성
4.필요 메소드 생성
4.필요 메소드 생성
create table member(
id varchar2(12) primary key,
passwd varchar2(12) not null,
name varchar2(10) not null,
jumin1 varchar2(6) not null,
jumin2 varchar2(7) not null,
email varchar2(30),
blog varchar2(50),
reg_date date not null
);
com.domain <- 자바빈
com.dao <- DAO
프로퍼티 부분만 입력하고
이것을 사용하면
자동적으로 getter와 setter가 생성됨
package com.domain; import java.sql.Timestamp; //자바빈 public class Member { //프로퍼티 private String id; private String passwd; private String name; private String jumin1; private String jumin2; private String email; private String blog; private Timestamp reg_date; //getter public String getId() { return id; } //setter public void setId(String id) { this.id = id; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJumin1() { return jumin1; } public void setJumin1(String jumin1) { this.jumin1 = jumin1; } public String getJumin2() { return jumin2; } public void setJumin2(String jumin2) { this.jumin2 = jumin2; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getBlog() { return blog; } public void setBlog(String blog) { this.blog = blog; } public Timestamp getReg_date() { return reg_date; } public void setReg_date(Timestamp reg_date) { this.reg_date = reg_date; } }
MemberDao.java
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import com.domain.Member; public class MemberDao { //싱글턴 패턴 //생성자의 접근지정자는 private 으로 지정 //static 한getInstance() 메소드 사용 //객체를 한번만 생성해서 공유하고자 싱글턴 패턴 구현 private static MemberDao instance = new MemberDao(); public static MemberDao getInstance(){ return instance; } private MemberDao(){ } //커넥션 풀로부터 커넥션을 할당받는 메소드 private Connection getConnection() throws Exception{ Context initCtx = new InitialContext(); Context envCtx =(Context)initCtx.lookup("java:comp/env"); DataSource ds = (DataSource)envCtx.lookup("jdbc/orcl"); return ds.getConnection(); } //throws Exception 넣는 이유는 db연동시 예외발생 확률이 높기때문 //회원가입 public void insertMember(Member member)throws Exception{ Connection conn = null; PreparedStatement pstmt = null; String sql = ""; int cnt = 0; try{ //커넥션 풀로부터 커넥션 할당 conn = getConnection(); sql = "insert into MEMBER values(?,?,?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(++cnt, member.getId()); pstmt.setString(++cnt, member.getPasswd()); pstmt.setString(++cnt, member.getName()); pstmt.setString(++cnt, member.getJumin1()); pstmt.setString(++cnt, member.getJumin2()); pstmt.setString(++cnt, member.getEmail()); pstmt.setString(++cnt, member.getBlog()); pstmt.setTimestamp(++cnt, member.getReg_date()); pstmt.executeUpdate(); }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(null,pstmt,conn); } } //회원 ID,비밀번호 체크 public int userCheck(String id,String passwd)throws Exception{ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql = ""; String dbpasswd=""; int x=-1; try{ conn=getConnection(); sql="select passwd from MEMBER where id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1,id); rs=pstmt.executeQuery(); if(rs.next()){ dbpasswd= rs.getString("passwd"); if(dbpasswd.equals(passwd)) x=1;//인증 성공 else x=0;//비밀번호 틀림 }else x= -1;//해당아이디 없음 }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(rs,pstmt,conn); } return x; } //회원상세 정보 public Member getMember(String id)throws Exception{ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; Member member =null; String sql = ""; try{ conn=getConnection(); sql = "select * from MEMBER where id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, id); rs = pstmt.executeQuery(); //rs ResultSet의 커서 위치는 행밖의 위치해 있기때문에 한건의 레코드를 가져오더라도 //next()메소드를 사용해서 빼낸다 if(rs.next()){ member = new Member(); member.setId(rs.getString("id")); member.setPasswd(rs.getString("Passwd")); member.setName(rs.getString("name")); member.setJumin1(rs.getString("jumin1")); member.setJumin2(rs.getString("jumin2")); member.setEmail(rs.getString("email")); member.setBlog(rs.getString("blog")); member.setReg_date(rs.getTimestamp("reg_date")); } }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(rs,pstmt,conn); } return member; } //회원정보 수정 public void updateMember(Member member)throws Exception{ Connection conn = null; PreparedStatement pstmt = null; String sql=""; try{ conn = getConnection(); sql = "update MEMBER set passwd=?,name=?,email=?,blog=? where id=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1,member.getPasswd()); pstmt.setString(2,member.getName()); pstmt.setString(3,member.getEmail()); pstmt.setString(4,member.getBlog()); pstmt.setString(5,member.getId()); pstmt.executeUpdate(); }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(null,pstmt,conn); } } //회원정보 삭제(탈퇴) public void deleteMember(String id)throws Exception{ Connection conn = null; PreparedStatement pstmt = null; String sql = null; try{ conn=getConnection(); sql = "delete from MEMBER where id=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1,id); pstmt.executeUpdate(); }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(null,pstmt,conn); } } //아이디 중복 체크 public int confirmId(String id)throws Exception{ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql = ""; int x=-1; try{ conn = getConnection(); sql = "select id from MEMBER where id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1,id); rs = pstmt.executeQuery(); if(rs.next()) x = 1;//해당 아이디 있음 else x= -1;//해당 아이디 없음 }catch(Exception ex){ ex.printStackTrace(); }finally{ execClose(rs,pstmt,conn); } return x; } //자원정리 public void execClose(ResultSet rs,PreparedStatement pstmt,Connection conn) throws Exception { if(rs != null)try{rs.close();}catch(SQLException ex){} if(pstmt != null)try{pstmt.close();}catch(SQLException ex){} if(conn != null)try{conn.close();}catch(SQLException ex){} //만들어진 역순으로 제거해야됨 conn -> pstmt -> rs로 만들어짐 } }
커넥션 풀
위치
회원 가입
기능 추가
회원가입 ID중복확인
e-mail , blog는 필수 입력사항이 아니므로 alert창을 띄우지 않음
회원가입 ID중복확인
inputForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원가입</title> <script type="text/javaScript"> function checkIt(){ var userinput = document.userinput; if(!userinput.id.value){ alert("ID를 입력하세요"); userinput.id.focus(); return false; } if(!userinput.passwd.value){ alert("비밀번호를 입력하세요") userinput.passwd.focus(); return false; } if(!userinput.name.value){ alert("사용자 이름을 입력하세요") userinput.name.focus(); return false; } if(!userinput.jumin1.value){ alert("주민등록 번호를 입력하세요") userinput.jumin1.focus(); return false; } if(!userinput.jumin2.value){ alert("주민등록 번호를 입력하세요") userinput.jumin2.focus(); return false; } return true; } //아이디 중복 여부 판단 function openConfirmid(userinput){ //아이디를 입력했는지 검사 if(userinput.id.value ==""){ alert("아이디를 입력하세요") userinput.id.focus(); return; } //url과 사용자 입력id를 조합합니다. url = "confirmId.jsp?id=" + userinput.id.value; //새로운 윈도우를 엽니다. open(url,"confirm","toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizeable=no,width=300,height=200"); } </script> </head> <body> <form method="post" action="inputPro.jsp" name="userinput" onSubmit="return checkIt()" > 사용자 ID: <input type="text" name="id" size="10" maxlength="12" > <input type="button" name="confirm_id" value="ID 중복확인" onClick="openConfirmid(this.form)"><br/> 비밀번호 : <input type="password" name="passwd" size="15" maxlength="12" ><br/> 사용자 이름 : <input type="text" name="name" size="15" maxlength="10" ><br/> 주민등록 번호 : <input type="text" name="jumin1" size="7" maxlength="6" > -<input type="text" name="jumin2" size="7" maxlength="7" ><br/> E-Mail : <input type="text" name="email" size="40" maxlength="30" ><br/> Blog : <input type="text" name="blog" size="60" maxlength="50" ><br/> <input type="submit" value="등 록"> <input type="reset" value="다시 입력"> </form> </body> </html>
inputPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "com.dao.MemberDao" %> <%@ page import = "java.sql.Timestamp" %> <% request.setCharacterEncoding("UTF-8"); %> <jsp:useBean id="member" class="com.domain.Member"> <jsp:setProperty name="member" property="*" /> </jsp:useBean> <% //등록일 member.setReg_date(new Timestamp(System.currentTimeMillis())); MemberDao manager = MemberDao.getInstance(); manager.insertMember(member); %> <script> alert("축하"); location.href="main.jsp"; </script>
confirmId.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "com.dao.MemberDao" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>ID 중복확인</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <% request.setCharacterEncoding("UTF-8"); String id = request.getParameter("id"); MemberDao manager = MemberDao.getInstance(); int check= manager.confirmId(id); %> <body> <% if(check == 1) { %> <table width="270" border="0" cellspacing="0" cellpadding="5"> <tr> <td height="39" ><%=id%>이미 사용중인 아이디입니다.</td> </tr> </table> <form name="checkForm" method="post" action="confirmId.jsp"> <table width="270" border="0" cellspacing="0" cellpadding="5"> <tr> <td align="center"> 다른 아이디를 선택하세요.<p> <input type="text" size="10" maxlength="12" name="id"> <input type="submit" value="ID중복확인"> </td> </tr> </table> </form> <% } else { %> <table width="270" border="0" cellspacing="0" cellpadding="5"> <tr> <td align="center"> <p>입력하신 <%=id%> 는 사용하실 수 있는 ID입니다. </p> <input type="button" value="닫기" onclick="setid()"> </td> </tr> </table> <% } %> </body> </html> <script type="text/javascript"> <!-- function setid() { opener.document.userinput.id.value="<%=id%>"; self.close(); } //--> </script>
회원탈퇴
login.jsp 실행
deletePro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.dao.MemberDao" %> <% if(session.getAttribute("memId")==null){ response.sendRedirect("main.jsp"); }else{ %> <% String id = request.getParameter("id"); String passwd = request.getParameter("passwd"); MemberDao manager = MemberDao.getInstance(); int check = manager.userCheck(id,passwd); if(check==1){ manager.deleteMember(id); session.invalidate(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원탈퇴</title> </head> <body> <h4>회원정보 삭제</h4> <div align="center">회원정보가 삭제되었습니다<br/> <input type="button" value="확인" onClick="location.href='main.jsp'"> </div> </body> </html> <% }else{ %> <script> alert("id 또는 비밀번호가 맞지 않습니다."); history.go(-1); </script> <% } } %>
deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% if(session.getAttribute("memId") == null){ response.sendRedirect("main.jsp"); }else{ %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><title>회원탈퇴</title> </head> <body> <form name="myform" action="deletePro.jsp" method="post"> 아이디 : <input type=password name="id" size="15" maxlength="12"><br/> 비밀번호 : <input type=password name="passwd" size="15" maxlength="12"><br/> <input type=submit value="회원탈퇴"> <input type="button" value="취 소" onclick="location.href='main.jsp'"> </form> </body> </html> <%}%>
회원정보 수정
이름 변경후
수정 5초뒤 main.jsp로
이동 수정 확인
이름 변경후
수정 5초뒤 main.jsp로
이동 수정 확인
비밀번호는 변경될 비밀번호이다(이미 검증하여 들어왔기때문)
요즘엔 이렇게 사용은 하지 않지만
따로 비밀번호 변경 jsp를 생성해야 될것이다.
modifyForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.dao.MemberDao,com.domain.Member" %> <% if(session.getAttribute("memId") == null){ response.sendRedirect("main.jsp"); }else{ %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>회원정보 수정</title> </head> <% String memId = (String)session.getAttribute("memId"); MemberDao manager = MemberDao.getInstance(); Member c = manager.getMember(memId); //이메일과 블로그는 입력하지 않아도 되는 값이어서 //회원정보 수정란에 null표시 방지 if(c.getEmail()==null){ c.setEmail(""); } if(c.getBlog()==null){ c.setBlog(""); } %> <body> <form method="post" action="modifyPro.jsp" name="userinput"> <h3>회원정보 수정</h3> 사용자 ID : <%=c.getId() %><br/> 비밀 번호 : <input type="password" name="passwd" size="10" maxlength="10" value="<%=c.getPasswd()%>"><br/> 사용자 이름 :<input type="text" name="name" size="15" maxlength="20" value="<%=c.getName() %>"><br/> 주민등록 번호:<%=c.getJumin1() %>-<%=c.getJumin2() %><br/> E-Mail :<input type="text" name="email" size="40" maxlength="30" value="<%=c.getEmail() %>"><br/> Blog :<input type="text" name="blog" size="60" maxlength="50" value="<%=c.getBlog() %>"><br/> <input type="submit" name="modify" value="수 정"> <input type="button" value="취 소" onclick="location.href='main.jsp'"> </form> </body> </html> <%}%>
modifyPro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.dao.MemberDao" %> <% if(session.getAttribute("memId")==null){ response.sendRedirect("main.jsp"); }else{ request.setCharacterEncoding("UTF-8"); %> <jsp:useBean id="member" class="com.domain.Member"> <jsp:setProperty name="member" property="*" /> </jsp:useBean> <% member.setId((String)session.getAttribute("memId")); MemberDao manager = MemberDao.getInstance(); manager.updateMember(member); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <!-- 5초뒤에 main.jsp을 새로고침 --> <meta http-equiv="Refresh" content="5;url=main.jsp"> <title>회원정보 수정</title> </head> <body> <h3>회원정보 수정</h3> <div align="center">회원정보가 정상적으로 수정되었습니다.</div> </body> </html> <%}%>
modifyForm.jsp
aa
'Oracle > 기본' 카테고리의 다른 글
ORACLE 자동생성 sequence (0) | 2012.02.10 |
---|---|
오라클 계정 생성 , 삭제 (0) | 2012.02.09 |
JSP ORACLE DELETE 레코드 삭제 (0) | 2012.02.09 |
JSP DB UPDATE (0) | 2012.02.08 |
JSP 생성한 db 보기 SELECT (0) | 2012.02.08 |