DAO: 데이터 접근을 위한 객체. SQL문을 관리, 쿼리를 DB에 보내 VO에 저장하는 역할을 맡음.

먼저 DAO Interface를 설계. DAO Interface안에는 CRUD(Create, Read, Update, Delete)와 같은 메서드가 있음. DAOImpl 객체가 Interface를 구현하면서 안에 있는 메서드를 오버라이드함.

각각의 메서드를 오버라이드 할 때 SQLAgent가 모두 들어감.


Enum이란? 상수 컬렉션을 정의하기 위한 특별한 자바 타입(special Java type used to define collections of constants). JenKov 블로그에 잘 설명되어있음. 

Enum을 통해 BD에 보내줄 SQL문을 작성.




package org.soju.foodstore.dao;

import org.soju.vo.StoreVO;


public interface FoodStoreDAO {

public void create(StoreVO vo) throws Exception;

public StoreVO read(Integer sno) throws Exception;

public void update(StoreVO vo) throws Exception;

public void delete(Integer vo) throws Exception;

}





package org.soju.foodstore.dao;

 

import org.soju.foodstore.util.SQLAgent;

import org.soju.vo.StoreVO;

 

public class FoodStoreDAOImpl implements FoodStoreDAO {

enum SQL{

INSERT(" insert into tbl_store(sno, sname, addr, keyword, tel, sdesc)"+

" values(seq_store.nextval, ?, ?, ?, ?, ?)"),

READ(" select * from tbl_store where sno = ?"),

DELETE(" delete from tbl_store where sno = ?"),

UPDATE(" UPDATE tbl_store SET sname = ?, ADDR = ?"+

", KEYWORD = ?, tel=?, sdesc = ? WHERE sno = ?");

String value;

SQL(String value){

this.value=value;

}

}

 

@Override

public void create(final StoreVO vo) throws Exception {

// sname, addr, keyword, tel, sdesc

new SQLAgent() {

@Override

protected void doJob() throws Exception {

pstmt = con.prepareStatement(SQL.INSERT.value);

pstmt.setString(1,vo.getSname());

pstmt.setString(2,vo.getAddr());

pstmt.setString(3,vo.getKeyword());

pstmt.setString(4,vo.getTel());

pstmt.setString(5,vo.getSdesc());

int resultCount = pstmt.executeUpdate();//항상 이게 나옴. 1이면 한 건 인서트 됐음

if (resultCount<1) {

throw new Exception("insert error");

}

}

}.doExecute();

}

 

@Override

public StoreVO read(Integer sno) throws Exception {

//sname, addr, keyword, tel, sdesc

final StoreVO vo = new StoreVO();

new SQLAgent() {

@Override

protected void doJob() throws Exception {

pstmt =  con.prepareStatement(SQL.READ.value);

pstmt.setInt(1,sno);

rs = pstmt.executeQuery();

rs.next();

 

vo.setSno(rs.getInt(1));

vo.setSname(rs.getString(2));

vo.setAddr(rs.getString(3));

vo.setKeyword(rs.getString(4));

vo.setSdesc(rs.getString(6));

vo.setRegdate(rs.getDate(7));

}

}.doExecute();

return vo;

}

 

@Override

public void update(StoreVO vo) throws Exception {

new SQLAgent() {

// UPDATE(" UPDATE tbl_store SET sname = ?, ADDR = ?"+

// ", KEYWORD = ?, tel=?', sdesc = ? WHERE sno = ?;");

@Override

protected void doJob() throws Exception {

try {

pstmt = con.prepareStatement(SQL.UPDATE.value);

pstmt.setString(1,vo.getSname());

pstmt.setString(2,vo.getAddr());

pstmt.setString(3,vo.getKeyword());

pstmt.setString(4,vo.getTel());

pstmt.setString(5,vo.getSdesc());

pstmt.setInt(6,vo.getSno());

int resultCount = pstmt.executeUpdate();//항상 이게 나옴. 1이면 한 건 인서트 됐음

if (resultCount<1) {

throw new Exception("insert error");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}.doExecute();

}

 

@Override

public void delete(Integer sno) throws Exception {

new SQLAgent() {

@Override

protected void doJob() throws Exception {ㄴ

pstmt = con.prepareStatement(SQL.DELETE.value);

pstmt.setInt(1,sno);

int resultCount = pstmt.executeUpdate();//항상 이게 나옴. 1이면 한 건 인서트 됐음

if (resultCount<1) {

throw new Exception("insert error");

}

}

}.doExecute();

}

}


================================================================================================

UserDAOImpl은 아직 다 구현되지 않음

package org.soju.foodstore.dao;

import org.soju.vo.UserVO;


public interface UserDAO {

public void create(UserVO vo) throws Exception;

public UserVO read(String uid) throws Exception;

public void update(UserVO vo) throws Exception;

public void delete(String uid) throws Exception;

}




package org.soju.foodstore.dao;


import java.sql.Date;

import java.util.Calendar;

import java.util.GregorianCalendar;


import org.soju.foodstore.dao.FoodStoreDAOImpl.SQL;

import org.soju.foodstore.util.SQLAgent;

import org.soju.vo.StoreVO;

import org.soju.vo.UserVO;


public class UserDAOImpl implements UserDAO {

enum SQL{

INSERT(" insert into tbl_user(user_id, uname, pw, BIRTH)"+

"values(?,?,?,?)"),

READ(" select * from tbl_user where user_id = ?"),

DELETE(" delete from tbl_user where user_id = ?"),

UPDATE(" UPDATE tbl_store SET sname = ?, ADDR = ?"+

", KEYWORD = ?, tel=?, sdesc = ? WHERE sno = ?");


String value;

SQL(String value){

this.value=value;

}

}

@Override

public void create(UserVO vo) throws Exception {

new SQLAgent() {

@Override

protected void doJob() throws Exception {

pstmt = con.prepareStatement(SQL.INSERT.value);

pstmt.setString(1,vo.getUser_id());

pstmt.setString(2,vo.getUname());

pstmt.setString(3,vo.getPw());

// pstmt.setDate(4,vo.getBirth(),new GregorianCalendar());

pstmt.setDate(4,vo.getBirth());

int resultCount = pstmt.executeUpdate();//항상 이게 나옴. 1이면 한 건 인서트 됐음

if (resultCount<1) {

throw new Exception("insert error");

}

}

}.doExecute();

}


@Override

public UserVO read(String uid) throws Exception {

final UserVO vo = new UserVO();

new SQLAgent() {

@Override

protected void doJob() throws Exception {

pstmt =  con.prepareStatement(SQL.READ.value);

pstmt.setString(1,uid);

rs = pstmt.executeQuery();

rs.next();

 

vo.setUser_id(rs.getString(1));

vo.setUname(rs.getString(2));

vo.setPw(rs.getString(3));

vo.setBirth(rs.getDate(4));

vo.setGender(rs.getString(5));

vo.setRegdate(rs.getDate(6));

}

}.doExecute();

return vo;

}


@Override

public void update(UserVO vo) throws Exception {

new SQLAgent() {

@Override

protected void doJob() throws Exception {

try {

pstmt = con.prepareStatement(SQL.UPDATE.value);

// pstmt.setString(1,vo.getSname());

// pstmt.setString(2,vo.getAddr());

// pstmt.setString(3,vo.getKeyword());

// pstmt.setString(4,vo.getTel());

// pstmt.setString(5,vo.getSdesc());

// pstmt.setInt(6,vo.getSno());

int resultCount = pstmt.executeUpdate();//항상 이게 나옴. 1이면 한 건 인서트 됐음

if (resultCount<1) {

throw new Exception("insert error");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}.doExecute();

}


@Override

public void delete(String uid) throws Exception {

new SQLAgent() {

@Override

protected void doJob() throws Exception {

pstmt = con.prepareStatement(SQL.DELETE.value);

pstmt.setString(1,uid);

int resultCount = pstmt.executeUpdate();//항상 이게 나옴. 1이면 한 건 인서트 됐음

if (resultCount<1) {

throw new Exception("insert error");

}

}

}.doExecute();

}

}






Posted by 타다키치
TAG , , , ,

댓글을 달아 주세요