Data Access Object의 약어로 서버에서 DB로 접근하여 관리하는 객체이다. (즉, DB관련 기능을 구현하는 곳이다.)
데이터 베이스에 접속하여 데이터 추가, 삭제, 수정 등의 작업을 하는 클래스.
일반적인 JSP 혹은 Servlet 페이지내에 위의 로직을 함께 기술할 수 도 있지만,
유지보수 및 코드의 모듈화를 위해서 별도의 DAO 클래스를 만들어서 사용한다.
1 2 3 4 5 6 7 8 9 10 11 12 | Class.forName(driver); connection = DriverManager.getConnection(url, uid, upw); int n; String query = "insert into member values (?, ?, ?, ?, ?)"; preparedStatement = connection.preparedStatement(query); preparedStatement.setString(1, "id"); preparedStatement.setString(2, "pw"); preparedStatement.setString(3, "name"); preparedStatement.setString(4, "sex"); preparedStatement.setString(5, age); n = preparedStatement.executeUpdate(); | cs |
이전에는 query에 다 적었지만 '?' 를 줘서 query문을 미리전송하고 후에 setting을 해주는 기능이다.
이를 통해서 가독성을 높일 수 있다.
1 2 3 4 5 6 7 8 9 10 11 | <Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:xe" username="scott" password="1234" name="jdbc/Oracle11g" type="javax.sql.DataSource" maxActive="50" maxWait="1000" /> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.sql.Connection; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; Context context = null; DataSource dataSource = null; Connection connection = null; try { context = new InitialContext(); dataSource = (DataSource)context.lookup("java:comp/env/jdbc/Oracle11g"); connection = dataSource.getConnection(); } catch (Exception e) { e.printStackTrace(); } | cs |
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 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("EUC-KR"); %> <% if(session.getAttribute("vaildMember") != null){%> <jsp:forward page="main.jsp"/> <%}%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Login</title> </head> <body> <h1>Login Page</h1> <hr> <form action="loginOK.jsp" method="post"> 아이디 : <input type="text" name="id" value="<%if (session.getAttribute("id") != null) out.print(session.getAttribute("id"));%>" size="10"><br /> 비밀번호 : <input type="password" name="pw" size="10"><br /> <input type="submit" value="로그인"> <input type="button" value="회원가입" onclick="window.location='join.jsp' "> </form> </body> </html> | cs |
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 | <%@page import="com.dbcplec.ex.MemberDTO"%> <%@page import="com.dbcplec.ex.MemberDAO"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("EUC-KR"); String id = request.getParameter("id"); String pw = request.getParameter("pw"); //싱글톤으로 dao객체를 생성한다. MemberDAO dao = MemberDAO.getInstance(); int loginCheck = dao.loginCheck(id, pw); if(loginCheck == -1){ %> <script> alert("아이디가 존재하지 않습니다."); history.go(-1); </script> <% }else if(loginCheck == 0){ %> <script> alert("비밀번호가 틀렸습니다."); history.go(-1); </script> <% }else if(loginCheck == 1){ MemberDTO dto = dao.getMember(id); if(dto == null){ %> <script> alert("존재하지 않은 회원 입니다."); history.go(-1); </script> <% }else{ session.setAttribute("id", id); session.setAttribute("name", dto.getName()); session.setAttribute("ValidMem", "yes"); response.sendRedirect("main.jsp"); } } %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Login Check Result</title> </head> <body> <h1>Login Check Result</h1> <hr> </body> </html> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Join Page</title> </head> <body> <h1>Join</h1> <hr> <form action="joinOK.jsp" method="post"> 아이디 : <input type="text" name="id" size="20"><br/> 비밀번호 : <input type="password" name="pw" size="20"><br/> 이름 : <input type="text" name="name" size="20"><br/> 이메일 : <input type="email" name="email" size="20"><br/> 주소 : <input type="text" name="address" size="20"><br/><br/> <input type="submit" value="회원가입"> <input type="button" value="취소" onclick="window.location='login.jsp' "> </form> </body> </html> | cs |
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 | <%@page import="com.dbcplec.ex.MemberDAO"%> <%@page import="java.sql.Timestamp"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("EUC-KR"); %> <jsp:useBean id="dto" class="com.dbcplec.ex.MemberDTO"/> <jsp:setProperty property="*" name="dto"/> <!-- dto의 모든 프로퍼티(*)을 넣겠다. --> <% dto.setmDate(new Timestamp(System.currentTimeMillis())); //--- 여기 까지 데이터를 set하는 과정 MemberDAO dao = MemberDAO.getInstance(); if(dao.confirmId(dto.getId()) == MemberDAO.MEMBER_EXISTENT){ %> <script language="javascript"> alert("아이디가 존재합니다."); history.back(); </script> <% }else{ int result = dao.insertMember(dto); if( result == MemberDAO.MEMBER_JOIN_SUCCESS){ session.setAttribute("id", dto.getId()); %> <script language="javascript"> alert("회원가입 성공!!"); document.location.href="login.jsp"; </script> <% }else{ %> <script> alert("회원가입 실패!!"); document.location.href="login.jsp"; </script> <% } } %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Join Check Page</title> </head> <body> <h1>Join Check Result</h1> <hr> </body> </html> | cs |
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 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% if(session.getAttribute("ValidMem") == null){ %> <jsp:forward page="login.jsp"/> <% } String name = (String)session.getAttribute("name"); String id = (String)session.getAttribute("id"); %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Member MainPage</title> </head> <body> <h1>Member Main Page</h1> <hr> <h2><%= name %>님 안녕하세요.</h2> <form action="logout.jsp" method="post"> <input type="submit" value="로그아웃"> <input type="button" value="정보수정" onclick="window.location='modify.jsp' "> </form> </body> </html> | cs |
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 | <%@page import="com.dbcplec.ex.MemberDTO"%> <%@page import="com.dbcplec.ex.MemberDAO"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("EUC-KR"); String id = (String)session.getAttribute("id"); MemberDAO dao = MemberDAO.getInstance(); MemberDTO dto = dao.getMember(id); %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Member Modify Page</title> </head> <body> <h1>Member Modify Page</h1> <hr> <form action="modifyOK.jsp" method="post"> 아이디 : <%= dto.getId() %><br/> 비밀번호 : <input type="password" name="pw" size="20"><br/> 이름 : <%=dto.getName() %><br/> 이메일 : <input type="email" name="email" size="20" value="<%=dto.getEmail() %>"><br/> 주소 : <input type="text" name="address" size="20" value="<%=dto.getAddress() %>"><br/><br/> <input type="submit" value="수정"> <input type="button" value="취소" onclick=" history.go(-1) "> </form> </body> </html> | cs |
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 | <%@page import="com.dbcplec.ex.MemberDAO"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("EUC-KR"); %> <jsp:useBean id="dto" class="com.dbcplec.ex.MemberDTO"/> <jsp:setProperty property="*" name="dto"/> <% String id = (String)session.getAttribute("id"); dto.setId(id); MemberDAO dao = MemberDAO.getInstance(); int result = dao.modifyMember(dto); if(result == 1){ %> <script> alert("회원정보가 수정되었습니다."); document.location.href="main.jsp"; </script> <% }else{ %> <script> alert("회원정보 수정에 실패하였습니다."); history.go(-1); </script> <% } %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> </body> </html> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% session.invalidate(); response.sendRedirect("login.jsp"); %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> </body> </html> | cs |
감사합니다...
EL(Expression Language) 예제 (0) | 2018.10.25 |
---|---|
파일 업로드 라이브러리(cos.jar) - multipart/form-data (0) | 2018.10.25 |
간단한 회원가입 및 회원정보 수정 (0) | 2018.10.24 |
JDBC 연동 후 DB 테스트 (0) | 2018.10.22 |
자바 빈(JAVA Bean) (0) | 2018.10.20 |
댓글 영역