[JSP] DAO, DTO / 컀λ„₯μ…˜ ν’€ (DBCP)


DAO / DTO


  • DAO

    • Data Access Object

    • DB에 직접 μ ‘κ·Όν•˜μ—¬ DB와 κ΄€λ ¨λœ λ‘œμ§μ„ μ²˜λ¦¬ν•˜λŠ” 객체

    • DB에 μ ‘μ†ν•΄μ„œ 데이터 μΆ”κ°€, μ‚­μ œ, μˆ˜μ • λ“±μ˜ μž‘μ—…μ„ ν•˜λŠ” 클래슀

    • μœ μ§€λ³΄μˆ˜, μ½”λ“œμ˜ λͺ¨λ“ˆν™”λ₯Ό μœ„ν•΄ λ³„λ„μ˜ DAO 클래슀λ₯Ό λ§Œλ“€μ–΄ μ‚¬μš©

  • DTO

    • Data Transfer Object

    • DAOκ°€ DB에 μ ‘κ·Όν•΄ κ°€μ Έμ˜¨ 데이터λ₯Ό 클래슀둜 λ§Œλ“€μ–΄μ„œ κ°€μ Έμ˜€λŠ” 객체

    • 계측 κ°„ 데이터 κ΅ν™˜μ΄ 이루어 질 수 μžˆλ„λ‘ ν•˜λŠ” 객체 > νŠΉλ³„ν•œ λ‘œμ§μ„ 가지지 μ•ŠλŠ” μˆœμˆ˜ν•œ 데이터 객체여야 함



컀λ„₯μ…˜ ν’€ (DBCP)


  • DataBase Connection Pool

  • ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ λ‹€μˆ˜μ˜ μš”μ²­μ΄ λ°œμƒν•  경우 λ°μ΄ν„°λ² μ΄μŠ€μ— λΆ€ν•˜κ°€ λ°œμƒν•˜κ³ , JSP와 DB μ—°κ²° μ‹œ 맀번 connectλ₯Ό μ—°κ²°ν•˜λŠ” λ²ˆκ±°λ‘œμ›€μ΄ μžˆλŠ”λ°, 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ 컀λ„₯μ…˜ 풀을 μ‚¬μš©ν•œλ‹€.

  • ν’€(Pool)에 미리 μƒμ„±λœ Connection듀을 ν΄λΌμ΄μ–ΈνŠΈλ“€μ΄ 접속 ν•  λ•Œ κ°€μ Έλ‹€ μ‚¬μš© -> λ‹€ μ“°κ³  λ‚œ 컀λ„₯μ…˜ κ°μ²΄λŠ” λ°˜ν™˜, λ‹€μ‹œ μž¬ν™œμš©

  • Servers > context.xml에 컀λ„₯μ…˜ ν’€λ‘œ μ—°κ²°ν•  DB 속성 μž‘μ„±

      <Resource
    
          name = "DataSource에 λŒ€ν•œ JNDI 이름, μ™ΈλΆ€μ—μ„œ 해당정보에 μ ‘κ·Όν•  수 μžˆλ„λ‘ ν•˜λŠ” 이름값"
          auth = "인증 주체"
          driverClassName = "μ—°κ²°ν•  λ°μ΄ν„°λ² μ΄μŠ€ μ’…λ₯˜μ— λ”°λ₯Έ λ“œλΌμ΄λ²„ 클래슀 이름"
          url = "접속할 λ°μ΄ν„°λ² μ΄μŠ€ μ£Όμ†Œμ™€ 포트 번호 및 SID"
          username = "λ°μ΄ν„°λ² μ΄μŠ€ 접속 ID"
          password = "λ°μ΄ν„°λ² μ΄μŠ€ 접속 PW"
          type = "λ°μ΄ν„°λ² μ΄μŠ€ μ’…λ₯˜λ³„ DataSource, μ›Ήμ—μ„œ μ‚¬μš©(name μ†μ„±μœΌλ‘œ 호좜)될 λ•Œ ν‘œμ‹œλ˜λŠ” 객체 νƒ€μž…"
          maxActive = "λ™μ‹œμ— μ΅œλŒ€λ‘œ λ°μ΄ν„°λ² μ΄μŠ€μ— μ—°κ²°ν•  수 μžˆλŠ” Connection 수"
          maxIdle = "λ™μ‹œμ— idle μƒνƒœλ‘œ λŒ€κΈ°ν•  수 μžˆλŠ” μ΅œλŒ€ 수"
          maxWait = "컀λ„₯μ…˜ 풀에 μ‚¬μš© κ°€λŠ₯ν•œ 컀λ„₯μ…˜μ΄ 없을 경우 μƒˆλ‘œμš΄ 연결이 생길 λ•ŒκΉŒμ§€ κΈ°λ‹€λ¦¬λŠ” μ΅œλŒ€ μ‹œκ°„"
    
      />
    



컀λ„₯μ…˜ ν’€ (DBCP)을 μ‚¬μš©ν•œ νšŒμ› 쑰회, μΆ”κ°€


  • mysql

      create table memberDto(
          uname		char(10),
          uid			char(10),
          upw			char(10),
          age			int,
          phoneNum	char(20)
            
      );
    
      insert into memberDto
      values
      ('김이름', 'abcd', '1234', 25, '010-1234-5678'),
      ('λ°•μ•„λ£Έ', 'qwerty', 'pw7878', 29, '010-8989-656'),
      ('ν™μ΄μž„', 'iddi', 'asd456', 33, '010-1346-7946');
    
      select * from memberDto;
    
  • Servers > context.xml

      ...
          <Resource
            
              name = "jdbc/dbconn"
              auth = "Container"
              driverClassName = "com.mysql.cj.jdbc.Driver"
              url = "jdbc:mysql://localhost:3306/uMember?useSSL=false&serverTimezone=Asia/Seoul&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true"
              username = "root"
              password = "1234"
              type = "javax.sql.DataSource"
              maxActive = "50"
              maxIdle = "10"
              maxWait = "5000"
            
          />
      </Context>    
    
  • MemberDto.java

      package dto;
    
      public class MemberDto {
    
          private String uname;
          private String uid;
          private String upw;
          private int age;
          private String phoneNum;
            
          public MemberDto() {};
            
          public MemberDto(String uname, String uid, String upw, int age, String phoneNum) {
              super();
              this.uname = uname;
              this.uid = uid;
              this.upw = upw;
              this.age = age;
              this.phoneNum = phoneNum;
          }
    
          public String getUname() {
              return uname;
          }
    
          public void setUname(String uname) {
              this.uname = uname;
          }
    
          public String getUid() {
              return uid;
          }
    
          public void setUid(String uid) {
              this.uid = uid;
          }
    
          public String getUpw() {
              return upw;
          }
    
          public void setUpw(String upw) {
              this.upw = upw;
          }
    
          public int getAge() {
              return age;
          }
    
          public void setAge(int age) {
              this.age = age;
          }
    
          public String getPhoneNum() {
              return phoneNum;
          }
    
          public void setPhoneNum(String phoneNum) {
              this.phoneNum = phoneNum;
          }
                
      }
    
  • MemberDao.java

      package dao;
    
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.util.ArrayList;
    
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.sql.DataSource;
    
      import dto.MemberDto;
    
      public class MemberDao {
            
          private DataSource datasource;
          private Connection conn;
          private PreparedStatement pstmt;
          private ResultSet rs;
          private Context context;
          private ArrayList<MemberDto> mdto = new ArrayList<>();
            
          public MemberDao() {
          }
            
          // DBCPμ—μ„œ Connection λ°˜ν™˜
          public Connection getConnection() {
              try {
                  context = new InitialContext();
                  datasource = (DataSource)context.lookup("java:comp/env/" + "jdbc/dbconn");
                  conn = datasource.getConnection();
                    
              } catch (Exception e) {
                  e.printStackTrace();
              }
                
              return conn;
          }
            
            
          // Select - MemberDto ν…Œμ΄λΈ”μ— μžˆλŠ” 데이터 리슀트 κ°€μ Έμ˜€κΈ°
          public ArrayList<MemberDto> memList(){
                
              try {
                    
                  Connection conn = this.getConnection();
                  String sql = "Select * from memberDto";
                  pstmt = conn.prepareStatement(sql);
                  rs = pstmt.executeQuery();
                    
                  while (rs.next()) {
                        
                      String uname = rs.getString("uname");
                      String uid = rs.getString("uid");
                      String upw = rs.getString("upw");
                      int age = rs.getInt("age");
                      String phoneNum = rs.getString("phoneNum");
                        
                      MemberDto memberDto = new MemberDto(uname, uid, upw, age, phoneNum);
                        
                      mdto.add(memberDto);
                  }
                    
              } catch (Exception e) {
                  e.printStackTrace();
              }
                
              return mdto;
                
          }
            
            
          // Update - μ‹ κ·œ νšŒμ› 정보 μ €μž₯ν•˜κΈ°
          public void addMember(MemberDto dto) {
                
              mdto.add(dto);
                
              try {
                    
                  Connection conn = this.getConnection();
                  String sql = "insert into memberDto values (?, ?, ?, ?, ?)";
                  pstmt = conn.prepareStatement(sql);
                  pstmt.setString(1, dto.getUname());
                  pstmt.setString(2, dto.getUid());
                  pstmt.setString(3, dto.getUpw());
                  pstmt.setInt(4, dto.getAge());
                  pstmt.setString(5, dto.getPhoneNum());
                    
                  int res = pstmt.executeUpdate();
                    
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
            
      }
    

[ Select - νšŒμ› 쑰회 ]

  • MemberList.jsp

      <%@page import="dto.MemberDto"%>
      <%@page import="java.util.ArrayList"%>
      <%@page import="dao.MemberDao"%>
      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html lang="ko">
      <head>
          <meta charset="UTF-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Member List</title>
      </head>
      <body>
          <table>
              <tr>
                  <th>이름</th>
                  <th>아이디</th>
                  <th>λΉ„λ°€λ²ˆν˜Έ</th>
                  <th>λ‚˜μ΄</th>
                  <th>μ „ν™”λ²ˆν˜Έ</th>
              </tr>
          <%
              MemberDao mdao = new MemberDao();
              ArrayList<MemberDto> mdto = mdao.memList();
                
              for(MemberDto memberDto : mdto){
                  String uname = memberDto.getUname();
                  String uid = memberDto.getUid();
                  String upw = memberDto.getUpw();
                  int age = memberDto.getAge();
                  String phoneNum = memberDto.getPhoneNum();
          %>
              <tr>
                  <td><%=uname %></td>
                  <td><%=uid %></td>
                  <td><%=upw %></td>
                  <td><%=age %></td>
                  <td><%=phoneNum %></td>
              </tr>
          <%
              }
          %>
          </table>
      </body>
      </html>
    

[ Update - νšŒμ› μΆ”κ°€ ]

  • AddMember.jsp

      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
      <!DOCTYPE html>
      <html lang="ko">
      <head>
          <meta charset="UTF-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>νšŒμ› μΆ”κ°€</title>
      </head>
      <body>
          <form action="Add.jsp">
              이름 : <input type="text" name="uname">
              <br><br>
              아이디 : <input type="text" name="uid">
              <br><br>
              λΉ„λ°€λ²ˆν˜Έ : <input type="text" name="upw">
              <br><br>
              λ‚˜μ΄ : <input type="text" name="age">
              <br><br>
              ν•Έλ“œν°λ²ˆν˜Έ : <input type="text" name="phoneNum">
              <br><br>
              <input type="submit" value="확인">
          </form>
      </body>
      </html>
    
  • Add.jsp

      <%@page import="dto.MemberDto"%>
      <%@page import="dao.MemberDao"%>
      <%@ page language="java" contentType="text/html; charset=UTF-8"
          pageEncoding="UTF-8"%>
            
      <%
          request.setCharacterEncoding("UTF-8");
    
          String uname = request.getParameter("uname");
          String uid = request.getParameter("uid");
          String upw = request.getParameter("upw");
          int age = Integer.parseInt(request.getParameter("age"));
          String phoneNum = request.getParameter("phoneNum");
            
          MemberDao mdao = new MemberDao();
          MemberDto mdto = new MemberDto();
            
          // μž…λ ₯ν•œ 정보 DTO 객체에 μ €μž₯
          mdto.setUname(uname);
          mdto.setUid(uid);
          mdto.setUpw(upw);
          mdto.setAge(age);
          mdto.setPhoneNum(phoneNum);
            
          mdao.addMember(mdto);
            
          response.sendRedirect("MemberList.jsp");
      %>
    

Categories:

JSP/Servlet