DAO, DTO

mandarindukΒ·2021λ…„ 3μ›” 17일
0

빈(bean)μ—λŠ” DAO, DTOκ°€ μžˆλ‹€.

DAO(Data Access Object)

  • DB 접근을 λͺ©μ μœΌλ‘œ ν•˜λŠ” 객체

DTO(Data Transfer Object)

  • DB에 μžˆλŠ” λ ˆμ½”λ“œλ₯Ό κ°μ²΄ν™”ν•œ 클래슀
  • 데이터가 ν¬ν•¨λœ 객체λ₯Ό λ‹€λ₯Έ μ‹œμŠ€ν…œμœΌλ‘œ μ „λ‹¬ν•˜λŠ” 역할을 ν•˜λŠ” 객체
DTO

package com.test.ex;

public class DTO {
    private String no;
    private String name;
    private String pw;
    
    public DTO() {
    	system.out.println("DTO 생성");
    }
    
    public DTO(String no, String name, String pw) {
    	this.no = no;
        this.name = name;
        this.pw = pw;
    }
    
    public String getNo() {
    	return no;
    }
    
    public String setNo(String no) {
        this.no = no;
    }
    
    ...
    
    
}
DAO

package com.test.ex;

public class DAO {
    Connection connect;
    PreparedStatement ps;
    ResultSet rs;
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    String id = "test", pw = "1234";
    
    public DAO() {
    	try {
        	Class.forName("oracle.jdbc.driver.OracleDriver");
            connect = DriverManager.getConnection(url, id, pw);
            System.out.println("DB μ—°κ²°!");
        } catch(Exception e) {
        	e.printStackTrace();
        }
    }
    
    public int insertStudent(StudentDTO sdto) throws SQLException {
    	String no = sdto.getNo(); 
        String name = sdto.getName(); 
        String pw = sdto.getPw(); 
        
        int n = this.insertStudent(no, name, pw);
        return n;
    }

    public int insertStudent(String no, String name, String pw) thorws SQLException{
    	String query = "insert into student values(?, ?, ?)";
        
        ps = connect.prepareStatement(query);
        ps.setString(1, no);
        ps.setString(2, name);
        ps.setString(3, pw);
    }
    
    int n =  ps.excuteUpadate();
    return n;
}
profile
front-end μ‹ μž… 개발자

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보

Powered by GraphCDN, the GraphQL CDN