DAO 를 활용한 쿼리문 작성 1 (insert, update, delete)

Jan 22, 2024
DAO 를 활용한 쿼리문 작성 1 (insert, update, delete)
 
 
이전 블로그와 연결된다.
 
public class UserDAO { public void insert(){} public void update(){} public void delete(){} }
 
UserDAO 에 insert, update, delete 3개의 메서드를 만들었다.
 
insert, update, delete 는 반이중 통신으로 DB에 요청을 하면 변경이 적용된 행 값만 받으면 된다.
리턴 값이 1이면 1개의 행이 영향을 받음, 0이면 0개의 행이 영향을 받은 것이고, -1 이면 오류가 난 것이다.
 
int num = pstmt.executeUpdate();
위의 코드를 통해 변경받은 행의 개수를 알 수 있다.
 
각 메서드에 쿼리문을 작성한다.
 

1. Insert문

쿼리문
public int insert(String username,String password,String phone){ Connection conn = DBConnection.getInstance(); // 소켓 연결 try{ String sql = "insert into user_tb(username,password,phone) values(?,?,?)"; //쿼리문 작성 PreparedStatement pstmt = conn.prepareStatement(sql); // 쿼리문을 버퍼에 담음 pstmt.setString(1,username); // 매개변수 값을 ? 에 대입 pstmt.setString(2,password); pstmt.setString(3,phone); int num = pstmt.executeUpdate(); // 쿼리문 전송, 응답은 num에 담음 return num ; // num = 1 , 1개의 테이블이 변화 , num = 0 테이블 변화없음 }catch (Exception e){ e.printStackTrace(); } return -1 ; // -1 은 오류 }
 
실행 코드
import org.junit.jupiter.api.Test; public class UserDAOTest { @Test public void inset_test() { //given = 매개변수 String username = "kim"; String password = "1234"; String phone = " 01012345678"; //when = 실행 UserDAO dao = new UserDAO(); int result = dao.insert(username, password, phone); //then = 결과 if (result == 1) { System.out.println("성공"); } else if (result == 0) { System.out.println("값의 변화가 없습니다."); } else { System.out.println("실패"); } } }
 
notion image
 
테스트 폴더에서 실행했을 때 정상적으로 실행 완료되었다.
 
notion image
 
테이블에 값이 추가되었다.

2. Delete

쿼리문
public int delete(int number){ Connection conn = DBConnection.getInstance(); try { String sql = "delete from user_tb where number = ? "; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1,number); int num = pstmt.executeUpdate(); // return num; } catch (Exception e) { e.printStackTrace(); } return -1; }
 
실행 코드
@Test public void delete_test(){ //given int number = 1; //when UserDAO dao = new UserDAO() ; int result = dao.delete(number); //then if (result == 1) { System.out.println("성공"); } else if (result == 0) { System.out.println(number + "번호를 찾을 수 없습니다."); } else { System.out.println("실패"); } }
 
notion image
 
정상적으로 데이터가 삭제되었다.
 

3. Update

쿼리문
public int update(String password, int number) { Connection conn = DBConnection.getInstance(); try { String sql = "UPDATE user_tb set password = ? WHERE number =? "; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, password); pstmt.setInt(2, number); int num = pstmt.executeUpdate(); // flush 코드 return num; } catch (Exception e) { e.printStackTrace(); //throw new RuntimeException(e); 이거는 오류를 메인에 던짐 } return -1; // 오류값을 }
 
실행 코드
@Test public void update(){ //given String password = "9876"; int number = 2; UserDAO dao = new UserDAO() ; int result = dao.update(password,number); //then if (result == 1) { System.out.println("성공"); } else if (result == 0) { System.out.println(number + "번호를 찾을 수 없습니다."); } else { System.out.println("실패"); } }
 
 
notion image
 
데이터가 변경되었다.
Share article
RSSPowered by inblog