어제만들었던 상품관리 프로그램에 이어서 검색, 수정, 삭제 기능을 활성화 시키기.
private void search() {
System.out.println(" 검색할 번호를 입력해주세요. ");
int pno = scan.nextInt();
Product p = svc.getProduct(pno);
System.out.println(p);
}
특정 상품 검색은 세부내용과 날짜까지 전부 나오도록 생성
검색할 pno가 필요
pno 값을 입력받아 매개변수로 전달 Product 객체 리턴
서비스 인터페이스에 getProduct(pno)를 추가하고 ProductServiceImpl 에 dao로 연결할 수 있게 구현
@Override
public Product getProduct(int pno) {
System.out.println("search serviceImpl success!! ");
return dao.selectProduct(pno);
}
DAO 인터페이스에 selectProduct를 추가하고 DAOImpl 에 구현
@Override
public Product selectProduct(int pno) {
System.out.println("search DAOImpl success!!");
query = "select * from product where pno = ?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, pno);
ResultSet rs = pst.executeQuery();
if(rs.next()){
Product p = new Product(
rs.getInt("pno"),
rs.getString("pname"),
rs.getInt("price"),
rs.getString("regdate"),
rs.getString("madeby"));
return p;
}
} catch (SQLException e) {
System.out.println("product error");
e.printStackTrace();
}
return null;
}
검색한 번호에 해당하는 상품을 출력할 수 있도록 쿼리문을 작성해주고
쿼리문을 실행해주는 객체 pst에 setInt해서 pno를 받아줌
ResultSet으로 리턴해주는 executeQuery()는 하나의 상품을 받더라도 if나 반복문으로 배열처리를 해주어야함
검색기능 끝.
private void modify() {
// 상품 수정 : 이름, 가격, 상세내용
System.out.println(" 수정할 상품 번호를 입력해주세요 ");
int pno = scan.nextInt();
System.out.println( "수정 상품이름: ");
String name = scan.next();
System.out.println(" 수정 상품가격: ");
int price = scan.nextInt();
scan.nextLine();
System.out.println(" 수정 상품상세내역: ");
String madeby = scan.nextLine();
Product p = new Product(name, price, madeby, pno);
int isOk = svc.update(p);
System.out.println("상품수정 > "+ ( (isOk > 0) ? "성공" : "실패" ));
}
수정은 등록과 비슷, insert가 update가 된다고 생각하고 상품번호를 받아주기만 하면됨.
서비스에 update(p)를 추가해주고 Impl 에서 다오로 연결
@Override
public int update(Product p) {
System.out.println("상품수정 serviceImpl success!! ");
return dao.update(p);
}
다오에 update 추가해주고 다오 Impl 에서 구현
@Override
public int update(Product p) {
System.out.println("update DAOImpl success!!");
query = "update product set pname=?, price=?, madeby=?, regdate=now() where pno=?";
try {
pst = conn.prepareStatement(query);
pst.setString(1, p.getPname());
pst.setInt(2, p.getPrice());
pst.setString(3, p.getMadeby());
pst.setInt(4, p.getPno());
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("update error");
e.printStackTrace();
}
return 0;
}
날짜는 원하면 수정 시간으로 바꿔주어도되고 안해도됨.
등록과 똑같이 ?로 넣은 값들을 순서에맞게 쿼리 실행객체에 set으로 넣어주고
executeUpdate()로 int 리턴을 받아주면 끝
private void delete() {
System.out.println(" 삭제할 상품 번호를 입력해주세요 ");
int pno = scan.nextInt();
int isOk = svc.delete(pno);
System.out.println("상품삭제 > "+ ( (isOk > 0) ? "성공" : "실패" ));
}
삭제도 pno 매개변수로 받아서 삭제할 상품번호를 찾아주고
@Override
public int delete(int pno) {
System.out.println("상품삭제 serviceImpl success!! ");
return dao.delete(pno);
}
@Override
public int delete(int pno) {
System.out.println("delete DAOImpl success!!");
query = "delete from product where pno=?";
try {
pst = conn.prepareStatement(query);
pst.setInt(1, pno);
return pst.executeUpdate();
} catch (SQLException e) {
System.out.println("delete error");
e.printStackTrace();
}
return 0;
}
삭제 쿼리구문 작성해서 쿼리실행객체 pst에 setInt로 pno값 넣어주고 pst.executeUpdate()로 int리턴받아서 isOk로 잘 작동하는지 확인해서 구현해주면 끝.
이렇게 상품관리프로그램을 자바와 DB를 연결해서 구현해보았는데 처음이라 구조가 익숙하지않지만, 흐름을 어느정도 이해했고 비슷한 구조로 더 연습해 볼 예정.
추가로 메서드명은 상관없지만 기능의 관련있게 명명 ( 네이밍 규칙을 정하고 지킴 )
'자바 수업 정리' 카테고리의 다른 글
State(장소) 관련 코드 (0) | 2024.10.20 |
---|---|
수업정리 17일차. (1) | 2024.10.15 |
수업정리 16일차. (3) | 2024.10.14 |
수업정리 15일차. (1) | 2024.10.11 |
수업정리 14일차. (0) | 2024.10.10 |