데이터 객체

✒️ 2025-06-24 00:25 내용 수정


DTO(Data Transform Object)

프로세스 간에 데이터를 전달하는 가변 객체

public class Person {
	private String name;
	private int age;

	public Person() {}

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	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;
	}
}

VO(Value Object)

한 개 또는 그 이상의 속성들을 묶고 값 그 자체를 표현하기 위한 불변 객체

final class Person {
	private String name;
	private int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
}

DAO(Data Access Object)

데이터에 접근하기 위한 객체

public class TestDAO {
	// singleton pattern
	private static TestDAO instance = new TestDAO();
	private TestDAO() {}
	public TestDAO getInstance() {return instance;}

	// 조회 결과 가져오기
	public List<TestDTO> selectList() {
		List<TestDTO> list = new ArrayList<>();
		String sql = "SELECT * FROM TEST_TABLE";

		try (Connection conn = DBConnection.getConnection();
			PreparedStatement pstmt = prepareStatement(sql);
			ResultSet result = pstmt.executeQuery();
		)
		{
			while(result.next()) {
				TestDTO dto = new TestDTO();
				dto.setName(result.getString("name"));
				dto.setNumber(result.getInt("number"));
				list.add(dto);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return list;
	}
}

예시

1. DB 접속용 클래스 생성

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnection {
	static DBConnection db;
	DataSource ds;

	// singleton pattern으로 생성
	public static DBConnection getInstance() {
		if(db == null) {
			db = new DBConnection();
		}
		return db;
	}

	// 생성자에서 Context 객체와 DataSource 초기화
	public DBConnection() {
		try {
			InitialContext ic = new InitialContext();
			Context ctx = (Context)ic.lookup("java:comp/env");
			ds = (DataSource)ctx.lookup("jdbc/oracle_test");
		
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	// 생성자에서 준비한 정보로 DB에 연결하여 Connection 객체 얻기
	public Connection getConnection() {
		Connection connec = null;
		try {
			connec = ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connec;
	}
}

2. DTO 생성

public class TestDTO {
	// 캡슐화를 위한 private 설정
	private String name;
	private int number;

	// 생성자
	public TestDTO() {}
	public TestDTO(String name, int number) {
		this.name = name;
		this.number = number;
	}

	// getter와 setter
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
}

3. DAO 생성

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import DTO.DBConnection;
import DTO.TestDTO;

public class TestDAO {

	static TestDAO dao = null;
	
	// singleton pattern 사용
	public static TestDAO getInstance() {
		if (dao == null) {
			dao = new TestDAO();
		}
		return dao;
	}

	// DB에서 조회 결과를 List에 저장하여 반환
	public List<TestDTO> selectList() {
	
		List<TestDTO> list = new ArrayList<>();
		Connection connec = null;
		PreparedStatement prestat = null;
		ResultSet result = null;
		String sql = "SELECT * FROM TEST_TABLE";

		try {
			// 1. Connection 얻어오기
			connec = DBConnection.getInstance().getConnection();
			
			// 2. sql문 얻어오기
			prestat = connec.prepareStatement(sql);
			
			// 3. sql문 실행 결과 처리 객체 얻어오기
			result = prestat.executeQuery();
		
			while(result.next()) {
				// DTO 객체 생성
				TestDTO dto = new TestDTO();
				
				// DTO의 필드에 sql문 결과 테이블의 속성 저장
				dto.setName(result.getString("name"));
				dto.setNumber(result.getInt("number"));
				
				// list에 DTO 저장
				list.add(dto);
			}
			
		} catch (Exception e) {
			
		} finally {
			// 항상 DB에 연관된 객체는 사용 후 닫아주기
			// 생성했던 역순으로 닫기
			try {
				if (result != null) {
					result.close();
				}
				if (prestat != null) {
					prestat.close();
				}
				if (connec != null) {
					connec.close();
				}
				
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
		return list;
	}
}