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개의 댓글