DAO = Data Access Object
-DB 접근 로직 수행
DTO = Data Transfer Object
-데이터만 모아서 DTO에 담음. (그냥 Model로 생각하면 편함)
DTO
-데이터 담는 객체
public class DTO {
private Integer id;
private String pw;
private String name;
private String phone;
public DTO(Integer id,String pw,String name,String phone)
{
this.id=id;
this.pw = pw;
this.name = name;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
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;
}
}
DAO
-DB에서 가져온 데이터를 DTO에 담아서 그 리스트를 반환하는 함수를 가짐
public class DAO {
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String uid = "baj2121";
private String upw = "wns9511";
private org.apache.tomcat.jdbc.pool.DataSource dataSource;
public DAO() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
// javax.naming.Context context = new InitialContext();
// dataSource = (org.apache.tomcat.jdbc.pool.DataSource)context.lookup("java:comp/env/jdbc/Oracle11g");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public ArrayList select() throws SQLException
{
ArrayList dtos = new ArrayList();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection(url,uid,upw);
//con = dataSource.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select * from premem");
while(rs.next())
{
Integer id = rs.getInt("id");
String pw = rs.getString("pw");
String name= rs.getString("name");
String phone = rs.getString("phone");
DTO d = new DTO(id,pw,name,phone);
dtos.add(d);
}
}
catch(Exception e)
{
e.getStackTrace();
}
finally {
if(rs!=null) rs.close();
if(stmt!=null)stmt.close();
}
return dtos;
}
}
'Jsp' 카테고리의 다른 글
커넥션 풀(DBCP) (0) | 2019.11.30 |
---|---|
Prestatement (0) | 2019.11.30 |
JDBC & Oracle DB (0) | 2019.11.30 |
자바 Bean (0) | 2019.11.27 |
예외 페이지 (0) | 2019.11.27 |