λΉ(bean)μλ DAO, DTOκ° μλ€.
DAO(Data Access Object)
DTO(Data Transfer Object)
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;
}