DAO는 데이터베이스의 데이터에 접근하기 위한 객체이다. 데이트베이스 접근을 위한 로직과 비즈니스 로직을 분리하기 위해 사용한다.
사용자는 필요한 Interface를 DAO에게 던지고 DAO는 이 Interface를 구현한 객체를 사용자가 편리하게 사용할 수 있도록 반환한다.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class HobbyDAO {
//SQL DELETE 메소드
public void deleteHobby(int id, int no) {
Connection conn = null;
PreparedStatement psmt = null;
try {
conn = DBConnectionManager.getConnection();
String sql = "DELETE FROM t_hobby_list"
+" WHERE id = ? AND no = ?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.setInt(2, no);
int cnt = psmt.executeUpdate();
System.out.println("처리결과:"+cnt);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
DBConnectionManager.close(null, psmt, conn);
}
}
//SQL UPDATE 메소드
public void updateHobby(int id, int no, String hobby, int prefer) {
Connection conn = null;
PreparedStatement psmt = null;
try {
conn = DBConnectionManager.getConnection();
String sql = "UPDATE t_hobby_list"
+" SET hobby = ?,"
+" prefer = ?"
+" WHERE id = ? AND no = ?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, hobby);
psmt.setInt(2, prefer);
psmt.setInt(3, id);
psmt.setInt(4, no);
int cnt = psmt.executeUpdate(); //insert, update, delete
System.out.println("처리결과:"+cnt);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
DBConnectionManager.close(null, psmt, conn);
}
}
//SQL INSERT 메소드
public void insertHobby(int id, String hobby, int prefer) {
Connection conn = null;
PreparedStatement psmt = null;
try {
conn = DBConnectionManager.getConnection();
String sql = "insert into t_hobby_list "
+ " values ( ?, (select NVL(MAX(no),0)+1 from t_hobby_list where id = ?), ?, ?)";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.setInt(2, id);
psmt.setString(3, hobby);
psmt.setInt(4, prefer);
int cnt = psmt.executeUpdate();
System.out.println("처리결과:"+cnt);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
DBConnectionManager.close(null, psmt, conn);
}
}
//SQL SELECT 메소드_여러줄
public List<HobbyDTO> selectHobbyById(int id) {
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
List<HobbyDTO> hobbyList = null;
try {
conn = DBConnectionManager.getConnection();
String sql = "SELECT * FROM t_hobby_list "
+ " WHERE id = ?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
rs = psmt.executeQuery();
hobbyList = new ArrayList<HobbyDTO>();
while(rs.next()) {
HobbyDTO hobbyItem = new HobbyDTO();
hobbyItem.setId(rs.getInt("id"));
hobbyItem.setNo(rs.getInt("no"));
hobbyItem.setHobby(rs.getString("hobby"));
hobbyItem.setPrefer(rs.getInt("prefer"));
hobbyList.add(hobbyItem);
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
DBConnectionManager.close(rs, psmt, conn);
}
return hobbyList;
}
//SQL SELECT 메소드2_한줄씩
public HobbyDTO selectHobbyByIdAndNo(int id, int no) {
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
HobbyDTO hobbyItem = null;
try {
//DB 커넥션
conn = DBConnectionManager.getConnection();
//실행 로직
String sql = "SELECT * FROM t_hobby_list "
+ " WHERE id = ? AND no = ?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.setInt(2, no);
rs = psmt.executeQuery();
if(rs.next()) { //데이터가 한줄이 있으면~
hobbyItem = new HobbyDTO(); //데이터가 없으면 null로 return하기 위해 if문 안에 객체 생성
hobbyItem.setId(rs.getInt("id"));
hobbyItem.setNo(rs.getInt("no"));
hobbyItem.setHobby(rs.getString("hobby"));
hobbyItem.setPrefer(rs.getInt("prefer"));
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
//DB 연결 닫기
DBConnectionManager.close(rs, psmt, conn);
}
return hobbyItem;
}
}
▲ DAO 클래스 예
계층 간 데이터 교환을 위한 자바 빈즈(Java Beans)이다. 데이터베이스 레코드의 데이터를 매핑하기 위한 데이터 객체를 말한다. DTO는 보통 로직을 가지고 있지 않고 데이터와 그 데이터 접근을 위한 getter, setter 메소드(+toString)만 가지고 있다.
import java.sql.Date;
public class EmpDTO {
int empno;
String ename;
String job;
int mgr;
Date hiredate; //java.sql.Date를 import 해야 한다.
String hiredate;
int sal;
int comm;
int deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "EmpDTO [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate="
+ hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}
}
▲ DTO 클래스 예