[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"); %>