Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발 일기

VO(Value Object) 클래스를 활용해서 DAO 클래스 만들기 본문

JAVA 연습장

VO(Value Object) 클래스를 활용해서 DAO 클래스 만들기

개발 일기 2021. 7. 25. 23:09

안녕하세용!

 

지난번에 DAO(Data Access Object)에 대해서 정리하고 해당 클래스를 생성 후 활용해 보았는데요.

이번에는 VO(Value Object)클래스도 함께 생성하여 DAO 클래스를 다시 만들어 보겠습니다.

 

VO(Value Object)란??

 - 데이터 그 자체로 의미 있는 것을 담고 있는 객체

 - 데이터베이스의 레코드에 대응하는 변수들을 가짐

 


1. VO 클래스 생성

(Address DB테이블의 desc)
 이름      널?       유형           
------- -------- ------------ 
NO      NOT NULL  NUMBER(4)    
ID                       VARCHAR2(20) 
NAME                  VARCHAR2(50) 
PHONE                 VARCHAR2(50) 
ADDRESS              VARCHAR2(60) 

 

 

Address VO 클래스 생성

데이터베이스 테이블의 column의 속성과 일치하도록 자바 클래스를 생성함 (VO)

public class Address {
	/*
	 * address table 컬럼과 대응하는 멤버필드를 가짐
	 */
	private int no;
	private String id;
	private String name;
	private String phone;
	private String address;

	public Address() {
		// TODO Auto-generated constructor stub
	}

	public Address(int no, String id, String name, String phone, String address) {
		super();
		this.no = no;
		this.id = id;
		this.name = name;
		this.phone = phone;
		this.address = address;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Address [no=" + no + ", id=" + id + ", name=" + name + ", phone=" + phone + ", address=" + address
				+ "]";
	}

}

 

 

 

2. VO 클래스를 활용한 DTO 클래스 생성

public class AddressDao3 {
	
	public int insert(Address address) throws Exception{
		String driverClass= "oracle.jdbc.OracleDriver";
		String url = "jdbc:oracle:thin:@182.237.126.19:1521:xe";
		String user = "DB접속정보";
		String password = "DB접속정보";
		
		Class.forName(driverClass);
		Connection con = DriverManager.getConnection(url,user,password);
		
		String insertSql = "insert into address values(?,?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(insertSql);
		
		pstmt.setInt(1, address.getNo());
		pstmt.setString(2, address.getId());
		pstmt.setString(3, address.getName());
		pstmt.setString(4, address.getPhone());
		pstmt.setString(5, address.getAddress());
		
		int insertRowCount = pstmt.executeUpdate();
		System.out.println(insertRowCount+"행 insert");
		pstmt.close();
		con.close();
		return insertRowCount;
	}
}

 

1. insert 메소드의 인자를 VO 클래스(Address)의 객체(address)로 준 뒤

2. PreparedStatement를 활용하여 address의 getMethod를 통해 파라메타(?) 값을 타입에 맞춰 대입 하도록 설정

 

 

 

3. DTO & VO 클래스를 활용한 MAIN

public class AddressDao3TestMain {

	public static void main(String[] args) throws Exception {
		AddressDao3 addressDao3 = new AddressDao3();

		Address insertAddress = new Address(8888, "TEST", "순끼", "486-9544", "강원도");
		addressDao3.insert(insertAddress);

	}

}

1. DTO 클래스(AddressDao3)의 객체(addressDao3) 생성

2. VO 클래스(Address)의 생성자를 활용하여 insert 테이블의 파라메타 값을 대입한 객체(insertAddress) 생성

3. DTO 객체(addressDao3)의 insert 메소드를 활용하며 VO 클래스(Address)의 객체(insertAddress)를 인자로 전달

 

 

 

VO클래스를 생성하여 DAO 클래스를 보다 객체지향적으로 만들어 보았습니다.

감사합니다.

 

Comments