본문 바로가기

기존카테고리/JSP

[회원가입] 싱글톤 패턴 방식 - 회원입력, ID중복검사



memJoin.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" src="member.js"></script>

</head>

<body>

<center>

<hr width = 500 size=2 color="blue">

<h2>회원 가입</h2>

<hr width = 500 size=2 color="blue">

<form action="memJoinOK.jsp" method="post" name="join_frm">

아이디 :<input type="text" name="id" size="20"><br />

비밀번호 :<input type="password" name="pw" size="20"><br/>

비밀번호확인 :<input type="password" name="pw_confirm" size="20"><br/>

이름 : <input type="text" name="name" size="20"><br />

메일 : <input type="text" name="email" size="20"><br />

주소 : <input type="text" name="addr" size="50"><br />

<input type="button" value="회원가입" onclick="mem_check()">&nbsp;&nbsp;

<input type="reset" value="취소">

</form>

</center>

</body>

</html>



--------------------------------------------------------------


MemDTO.java


package com.test.ex;


import java.sql.Timestamp;


public class MemDTO {

private String id;

private String pw;

private String name;

private String email;

private String addr;

private Timestamp rDate;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getPw() {

return pw;

}

public void setPw(String pw) {

this.pw = pw;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public String getAddr() {

return addr;

}

public void setAddr(String addr) {

this.addr = addr;

}

public Timestamp getrDate() {

return rDate;

}

public void setrDate(Timestamp rDate) {

this.rDate = rDate;

}

}




------------------------------------------------------------------------------------------------------------


MemDAO.java


package com.test.ex;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.ArrayList;


import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;


public class MemDAO {

public static final int MEM_NONEXIST = 0;

public static final int MEM_EXIST = 1;

public static final int MEM_JOIN_SUCCESS = 1;

public static final int MEM_JOIN_FAIL = 0;

public static final int MEM_LOGIN_SUCCESS = 1;

public static final int MEM_LOGIN_PW_NOT = 0;

public static final int MEM_LOGIN_NOT = -1;

private static MemDAO memIns = new MemDAO();

private MemDAO(){

} //생성자 , 싱글톤패턴으로 하기 위해 생성자를 private로 설정

public static MemDAO getInstance(){

return memIns;                                            // 자신을 부르는 객체생성

}

public int insertMember(MemDTO mdto){

int n = 0;

Connection dbcon = null;

PreparedStatement pstmt = null;

String sql = "insert into member1 values(?,?,?,?,?,?)";

try{

dbcon = getConnection();

pstmt = dbcon.prepareStatement(sql);

pstmt.setString(1, mdto.getId());

pstmt.setString(2, mdto.getPw());

pstmt.setString(3, mdto.getName());

pstmt.setString(4, mdto.getEmail());

pstmt.setString(5, mdto.getAddr());

pstmt.setTimestamp(6, mdto.getrDate());

pstmt.executeUpdate();

n = MemDAO.MEM_JOIN_SUCCESS;

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(pstmt !=null) pstmt.close();

if(dbcon !=null) dbcon.close();

}catch(Exception e){

e.printStackTrace();

}

}

return n;

}

public int checkId(String id){

int n =0;

Connection dbcon = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

String sql = "select id from member1 where id = ?";

try{

dbcon = getConnection();

pstmt = dbcon.prepareStatement(sql);

pstmt.setString(1, id);

rs = pstmt.executeQuery();

if(rs.next()){

n = MemDAO.MEM_EXIST;

}else{

  n = MemDAO.MEM_NONEXIST;

}

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(pstmt !=null) pstmt.close();

if(dbcon !=null) dbcon.close();

}catch(Exception e){

e.printStackTrace();

}

}

return n;

}

public int memberCheck(String id, String pw){

int n = 0;

String rPw; 

Connection dbcon = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

String sql = "select pw from member1 where id = ?";

try{

dbcon = getConnection();

pstmt = dbcon.prepareStatement(sql);

pstmt.setString(1, id);

rs=pstmt.executeQuery();

if(rs.next()){

rPw = rs.getString("pw");

if(rPw.equals(pw)){

n = MemDAO.MEM_LOGIN_SUCCESS; // 로그인 성공

}else{

n = MemDAO.MEM_LOGIN_PW_NOT; // 비밀번호가 틀림

}

}else{

n = MemDAO.MEM_LOGIN_NOT; //회원이 아님. 

}

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(rs !=null) rs.close();

if(pstmt !=null) pstmt.close();

if(dbcon !=null) dbcon.close();

}catch(Exception e){

e.printStackTrace();

}

}

return n;

}

public MemDTO getMember(String id){

MemDTO mdto = null;

Connection dbcon = null;

PreparedStatement pstmt =null;

ResultSet rs = null;

String sql = "select * from member1 where id = ?";

try{

dbcon = getConnection();

pstmt = dbcon.prepareStatement(sql);

pstmt.setString(1,id);

rs = pstmt.executeQuery();


if(rs.next()){

mdto = new MemDTO();

mdto.setId(rs.getString("id"));

mdto.setPw(rs.getString("pw"));

mdto.setName(rs.getString("name"));

mdto.setEmail(rs.getString("email"));

mdto.setAddr(rs.getString("addr"));

mdto.setrDate(rs.getTimestamp("rDate"));

}

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(rs !=null) rs.close();

if(pstmt !=null) pstmt.close();

if(dbcon !=null) dbcon.close();

}catch(Exception e){

e.printStackTrace();

}

}

return mdto; 

}

public int updateMember(MemDTO mdto){

int n = 0;

Connection dbcon = null;

PreparedStatement pstmt =null;

String sql = "update member1 set name=?, email=?, addr=? where id=?";

try{

dbcon = getConnection();

pstmt = dbcon.prepareStatement(sql);

pstmt.setString(1, mdto.getName());

pstmt.setString(2, mdto.getEmail());

pstmt.setString(3, mdto.getAddr());

pstmt.setString(4, mdto.getId());

n=pstmt.executeUpdate();

}catch(Exception e){

e.printStackTrace();

}finally{

try{

if(pstmt !=null) pstmt.close();

if(dbcon !=null) dbcon.close();

}catch(Exception e){

e.printStackTrace();

}

}

return n;

}

private Connection getConnection(){

Context ctx = null;

DataSource ds = null;

Connection dbcon = null;

try{

ctx = new InitialContext();

ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Oracle11g");

dbcon = ds.getConnection();

}catch(Exception e){

e.printStackTrace();

}

return dbcon;

}

}



'기존카테고리 > JSP' 카테고리의 다른 글

EL(Expression Language)  (0) 2017.06.13
파일업로드  (0) 2017.06.13
커넥션 풀 설정 방법  (0) 2017.06.07
리스트 출력 DTO,DAO,memberView.jsp  (0) 2017.06.07
회원정보 수정 및 로그아웃  (0) 2017.06.07