1) DAO (Data Access Object)
DAO๋ DB์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํ ๊ฐ์ฒด.
์ง์ DB์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ , ์ญ์ , ์กฐํ ๋ฑ ์กฐ์ํ ์ ์์
DB ์ ๊ทผ ๋ก์ง๊ณผ ๋น์ฆ๋์ค ๋ก์ง์ ๋ถ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ.
์ดํ ๋์ DAO:
DeptDAO.java
package com.my;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DeptDAO {
// Singleton
private static DeptDAO dao = new DeptDAO();
private DeptDAO(){ }
public static DeptDAO getInstance() { return dao; }
/////////////////
DeptVO selectDept(int deptno, Connection conn) {
DeptVO dept = null;
try {
//String sql = "select * from dept where deptno = "+ deptno;
String sql = "select * from dept10 where deptno = ?";
//Statement stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
dept = new DeptVO(rs.getInt(1),rs.getString(2),rs.getString("loc"));
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
}
pstmt.close();
//stmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return dept;
}
int insertDept(DeptVO deptObj, Connection conn) {
int resultCount = 0;
DeptVO dept = null;
try {
String sql = "insert into dept10 values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptObj.getDeptno());
pstmt.setString(2, deptObj.getDname());
pstmt.setString(3, deptObj.getLoc());
resultCount = pstmt.executeUpdate();
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return resultCount;
}
int updateDept(DeptVO deptObj,Connection conn) {
int resultCount = 0;
try {
String sql = "update dept10 set loc = ? where deptno = ?"; ;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(2, deptObj.getDeptno());
pstmt.setString(1, deptObj.getLoc());
resultCount = pstmt.executeUpdate();
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return resultCount;
}
int deleteDept(int deptno,Connection conn ) {
int resultCount = 0;
try {
String sql = "delete from dept10 where deptno = ?"; ;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
resultCount = pstmt.executeUpdate();
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return resultCount;
}
List<DeptVO> listDept(Connection conn) {
List<DeptVO> deptList = new ArrayList<DeptVO>();
try {
String sql = "select * from dept10";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
deptList.add(new DeptVO(rs.getInt(1),rs.getString(2),rs.getString(3)));
}
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return deptList;
}
}
2) DTO (Data Transfer Object)
๊ณ์ธต๊ฐ(Controller, View, Business Layer) ๋ฐ์ดํฐ ๊ตํ์ ์ํ ์๋ฐ๋น์ฆ(....??)๋ฅผ ์๋ฏธํจ
๋ก์ง์ ๊ฐ์ง์ง ์๊ณ , getter / setter ๋ฉ์๋๋ง ๊ฐ์ง ํด๋์ค์.
- ์๋ฐ๋น์ฆ: ์๋ฐ๋ก ์์ฑ๋ ์ํํธ์จ์ด ์ปดํฌ๋ํธ๋ฅผ ์ง์นญํ๋ ๋จ์ด
3) VO (Value Object)
๋ฐ์ดํฐ ๊ทธ ์์ฒด๋ก ์๋ฏธ์๋ ๊ฒ์ ๋ด๊ณ ์๋ ๊ฐ์ฒด
Read only ์์ฑ์ ๊ฐ์ง ๊ฐ์ฒด
RDB์ ๋ ์ฝ๋์ ๋์๋๋ ์๋ฐ ํด๋์ค
๊ฐ๋จํ ๋ ๋ฆฝ์ฒด( Entity )๋ฅผ ์๋ฏธํ๋ ์์ ๊ฐ์ฒด
VO์ ํต์ฌ ์ญํ ์ equals()์ hashcode() ๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ๋ ๊ฒ. (....??)
์ดํ ๋์ VO:
DeptVO.java
package com.my;
// ๋ฐ์ดํฐ ๊ฐ์ฒด
public class DeptVO {
//๋ฉค๋ฒ ํ๋
private int deptno;
private String dname;
private String loc;
// ์์ฑ์
DeptVO(){ } // ์๋๋ ๊ฒ
DeptVO(int deptno, String dname, String loc){
this.deptno = deptno; this.dname = dname; this.loc = loc; }
// ๋ฉค๋ฒ ๋ฉ์๋ ( ๋ฉ๋ด source์์ --> Generate getters and setter์์ ์๋ ์์ฑ)
public int getDeptno() { return deptno; }
public String getDname() { return dname; }
public String getLoc() { return loc; }
@Override
public String toString() {
return "DeptVO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", toString()=" + super.toString()
+ "]";
}
}
Reference:
https://genesis8.tistory.com/214
https://velog.io/@ha0kim/DAO-DTO-VO-%EC%B0%A8%EC%9D%B4
'PlayData > MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day 18 | MySQL] DAO, VO๋ฑ (์ดํด ing) (2) | 2021.12.02 |
---|---|
[Day 16 | MySQL] Table, Variable, ๋ฐ์ดํฐ ๋ชจ๋ธ๋ง, (0) | 2021.12.01 |
[Day 15 | MySQL] SubQuery, DML, Table and ๊ณผ์ (0) | 2021.11.29 |
[Day14 | MySQL] Join(ANSI Join, inner join, outer join, cross join etc.) (0) | 2021.11.26 |
[Day13 | MySQL] SQL ๊ธฐ๋ณธ RDB, where, groupby, having (0) | 2021.11.25 |