회원가입 (3.DBConnection.java, 4.MemberBean.java 5.MemberDAO.java)
참고사이트 : https://all-record.tistory.com/114?category=733042
1.JoinForm.jsp : 회원가입화면
2.join_style.css : JoinForm과 JoinPro는 동일한 css를 사용하므로 별도로 파일 분리
3.DBConnection.java : DB커넥션을 담당하는 클래스 (JNDI를 통해 커넥션을 가져옴)
4.MemberBean.java : 회원정보를 전달할 자바빈
5.MemberDAO.java : JSP_MEMBER 테이블의 데이터 처리를 담당하는 클래스
6.JoinPro.jsp : 파라미터 값을 넘겨받아 회원가입을 처리하는 JSP
그외
-web.xml : JNDI 리소스 사용 설정을 위한 xml파일
-ojdbc6.jar : 오라클 JDBC 드라이버
-tomcat-dbcp.jar : 커넥션 풀 관련 라이브러
3.DBConnection.java(JNDI를 통해 커넥션을 가져옴)
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 31 32 33 | package jsp.util; import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; // 커넥션을 얻어오는 클래스 - JNDI public class DBConnection { public static Connection getConnection() throws SQLException, NamingException, ClassNotFoundException{ Context initCtx = new InitialContext(); //initCtx의 lookup메서드를 이용해서 "java:comp/env" 에 해당하는 객체를 찾아서 evnCtx에 삽입 Context envCtx = (Context) initCtx.lookup("java:comp/env"); //System.out.println("커넥션 된거냐 ??? " + envCtx); //envCtx의 lookup메서드를 이용해서 "jdbc/orcl"에 해당하는 객체를 찾아서 ds에 삽입 DataSource ds = (DataSource) envCtx.lookup("jdbc/orcl"); //System.out.println("커넥션 된거냐 ??? " + ds); //getConnection메서드를 이용해서 커넥션 풀로 부터 커넥션 객체를 얻어내어 conn변수에 저장 Connection conn = ds.getConnection(); //System.out.println("커넥션 된거냐 ??? " + conn); return conn; } } | cs |
4.MemberBean.java (회원정보를 전달할 자바빈)
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package jsp.member.model; import java.sql.Timestamp; public class MemberBean { private String id; // 아이디 private String password; // 비밀번호 private String name; // 이름 private String gender; // 성별 private String birthyy; // 생일 - 년 private String birthmm; // 생일 - 월 private String birthdd; // 생일 - 일 private String mail1; // 이메일 - @ 앞부분 private String mail2; // 이메일 - @ 뒷부분 private String phone; // 전화 private String address; // 주소 private Timestamp reg; // 가입일 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getBirthyy() { return birthyy; } public void setBirthyy(String birthyy) { this.birthyy = birthyy; } public String getBirthmm() { return birthmm; } public void setBirthmm(String birthmm) { this.birthmm = birthmm; } public String getBirthdd() { return birthdd; } public void setBirthdd(String birthdd) { this.birthdd = birthdd; } public String getMail1() { return mail1; } public void setMail1(String mail1) { this.mail1 = mail1; } public String getMail2() { return mail2; } public void setMail2(String mail2) { this.mail2 = mail2; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Timestamp getReg() { return reg; } public void setReg(Timestamp reg) { this.reg = reg; } // @Override //public String toString() { // return "MemberBean [id=" + id + ", password=" + password + ", name=" + name + ", gender=" + gender // + ", birthyy=" + birthyy + ", birthmm=" + birthmm + ", birthdd=" + birthdd + ", mail1=" + mail1 // + ", mail2=" + mail2 + ", phone=" + phone + ", address=" + address + ", reg=" + reg + "]"; //} } | cs |
5.MemberDAO.java (JSP_MEMBER 테이블의 데이터 처리를 담당하는 클래스)
DAO란 DB와 관련된 CRUD(등록,검색,수정,삭제) 작업을 처리한다. DAO는 일반적으로 DB테이블 당 1개를 작성한다.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | package jsp.member.model; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Date; import javax.naming.NamingException; import jsp.util.DBConnection; /* Data Access Object * 테이블 당 한개의 DAO를 작성한다. * * JSP_MEMBER 테이블과 연관된 DAO로 * 회원 데이터를 처리하는 클래스이다. */ public class MemberDAO { private static MemberDAO instance; // 싱글톤 패턴 private MemberDAO(){} public static MemberDAO getInstance(){ if(instance==null) instance=new MemberDAO(); return instance; } // String -> Date로 변경하는 메서드 // 문자열로된 생년월일을 Date로 변경하기 위해 필요 // java.util.Date클래스로는 오라클의 Date형식과 연동할 수 없다. // Oracle의 date형식과 연동되는 java의 Date는 java.sql.Date 클래스이다. public Date stringToDate(MemberBean member) { String year = member.getBirthyy(); String month = member.getBirthmm(); String day = member.getBirthdd(); System.out.println(member.getBirthyy()); System.out.println(member.getBirthmm()); System.out.println(member.getBirthdd()); //Date birthday = Date.valueOf("2016"+"-"+"11"+"-"+"22"); /* * 123 01 11 */ Date birthday = Date.valueOf(year+"-"+month+"-"+day); return birthday; } // end stringToDate() // 회원정보를 JSP_MEMBER 테이블에 저장하는 메서드 public void insertMember(MemberBean member) throws SQLException { Connection conn = null; PreparedStatement pstmt = null; // System.out.println("멤버출력하기 : " +member); // System.out.println("멤버출력하기 : " +member.toString()); try { // 커넥션을 가져온다. conn = DBConnection.getConnection(); System.out.println("커넥션 출력 : " + conn); // 자동 커밋을 false로 한다. conn.setAutoCommit(false); // 쿼리 생성한다. // 가입일의 경우 자동으로 세팅되게 하기 위해 sysdate를 사용 StringBuffer sql = new StringBuffer(); sql.append("insert into JSP_MEMBER values"); sql.append("(?, ?, ?, ?, ?, ?, ?, ?, sysdate)"); // stringToDate(member); /* * StringBuffer에 담긴 값을 얻으려면 toString()메서드를 * 이용해야 한다. */ pstmt = conn.prepareStatement(sql.toString()); pstmt.setString(1, member.getId()); pstmt.setString(2, member.getPassword()); pstmt.setString(3, member.getName()); pstmt.setString(4, member.getGender()); pstmt.setDate(5, stringToDate(member)); pstmt.setString(6, member.getMail1()+"@"+member.getMail2()); pstmt.setString(7, member.getPhone()); pstmt.setString(8, member.getAddress()); // 쿼리 실행 pstmt.executeUpdate(); // 완료시 커밋 conn.commit(); } catch (ClassNotFoundException | NamingException | SQLException sqle) { // } catch (Exception E) { //E.printStackTrace(); // 오류시 롤백 conn.rollback(); throw new RuntimeException(sqle.getMessage()); } finally { // Connection, PreparedStatement를 닫는다. try{ if ( pstmt != null ){ pstmt.close(); pstmt=null; } if ( conn != null ){ conn.close(); conn=null; } }catch(Exception e){ throw new RuntimeException(e.getMessage()); } } // end try~catch } // end insertMember() } | cs |
*싱글톤패턴(SINGLETON PATTERN)
싱글턴패턴이란 인스턴스를 하나만 만들어 사용하기 위한 패턴이다. 커넥션 풀, 스레드 풀, 디바이스 설정 객체 등의 경우 인스턴스를 여러개 만들게 되면 자원을 낭비하게 되거나 버그를 발생시킬 수 있으므로 오직 하나만 생성하고 그 인스턴스를 사용하도록 하는것이 목적이다.
애플리케이션이 시작될 때 어떤 클래스가 최초 한번만 메로리를 할당하고 그 메모리에 인스턴스를 만들어 사용하는 디자인패턴.
private MemberDAO(){}
* try catch(예외 처리 코드)
1 2 3 4 5 6 7 8 9 10 | try{ //에러가 발생할 수 있는 코드 throw new Exception(); //강제 에러 출력 }catch (Exception e){ //에러시 수행 e.printStackTrace(); //오류 출력(방법은 여러가지) throw e; //최상위 클래스가 아니라면 무조건 던져주자 }finally{ //무조건 수행 } | cs |