반응형
[1] 테스트 환경
hardware | |
cpu : intel i7-5820k, gpu : nvidia gtx 970, ram : ddr4 16GB, mainboard : x99a chipset 2011V3 board, secondary storage : 1TB SSD, 4TB HDD |
OS |
windows pro 64bits |
software |
jdk version 1.8.0_301 eclipse version 2021-09 (4.21.0) apache-tomcat version 9.0.54 oracle |
[] DAO & DTO
DAO(Data Access Object) : DB 접근하는 기능을 모듈화 해서 빼놓는 것이다. DB에 접속해서 데이터(CRUD)의 작업을 하는 클래스 JSP 및 Servlet 페이지 내에 로직을 기술해서 사용할 수도 있지만, 코드의 간결화 및 모듈화 유지보수 등의 목적을 위해서 별도의 DAO 클래스를 생성해서 사용하는게 좋다.
DTO(Data Transfer Object) : DB에 있는 데이터를 Java 형태로 만들어주는 중간매체(변환)이다. DataBase에 있는 데이터 형태와 Java에서 관리하는 데이터 형태가 다르기 때문에 데이터 가공을 해주는 기능
[] DAO & DTO 예제
() DAO
package com.jsp.dbcp;
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;
public class KGMemberDAO {
private DataSource ds;
// Connection pool을 관리하는 객체
public KGMemberDAO() {
try {
Context context = new InitialContext();
// JNDI 서비스를 제공하는 객체를 생성한다.
// JNDI : Java Naming & Directory Interface
// "이름" 을 가지고 데이터베이스 정보(객체)를 얻을 수 있는 API
ds = (DataSource)context.lookup("java:comp/env/jdbc/oracle");
}catch(Exception e) {
e.printStackTrace();
}
}
public ArrayList<KGMemberDTO> memberList(){
ArrayList<KGMemberDTO> list = new ArrayList<KGMemberDTO>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
// DataSource 를 통해 Connection 객체를 얻는다.
pstmt = conn.prepareStatement("select * from KGMember");
rs = pstmt.executeQuery();
while(rs.next()) {
String id = rs.getString("id");
String pw = rs.getString("pw");
String name = rs.getString("name");
int age = rs.getInt("age");
String phone = rs.getString("phone");
list.add(new KGMemberDTO(id, pw, name, age, phone));
}
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(rs != null)rs.close();
if(pstmt != null)pstmt.close();
if(conn != null)conn.close();
}catch(Exception e2) {}
}
return list;
}
}
()DTO 예제
package com.jsp.dbcp;
public class KGMemberDTO {
private String id;
private String pw;
private String name;
private int age;
private String phone;
public KGMemberDTO() {}
public KGMemberDTO(String id, String pw, String name, int age, String phone) {
super();
this.id = id;
this.pw = pw;
this.name = name;
this.age = age;
this.phone = phone;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
반응형