package model;
public class ProductVO {
private int code;
private String title;
private int price;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "ProductVO [code=" + code + ", title=" + title + ", price=" + price + "]";
}
}
model/ProductDAO
packagemodel;importjava.sql.*;importjava.util.*;publicclassProductDAO{// 수정publicvoidupdate(ProductVOvo){try{Stringsql="update tbl_product set title=?,price=? where code=?";PreparedStatementps=Database.CON.prepareStatement(sql);ps.setString(1,vo.getTitle());ps.setInt(2,vo.getPrice());ps.setInt(3,vo.getCode());ps.execute();}catch(Exceptione){System.out.println("update:"+e.toString());}}//정보읽기publicProductVOread(intcode){ProductVOvo=newProductVO();try{Stringsql="select * from tbl_product where code=?";PreparedStatementps=Database.CON.prepareStatement(sql);ps.setInt(1,code);ResultSetrs=ps.executeQuery();if(rs.next()){vo.setCode(rs.getInt("code"));vo.setTitle(rs.getString("title"));vo.setPrice(rs.getInt("price"));System.out.println(vo.toString());}}catch(Exceptione){System.out.println("read : "+e.toString());}returnvo;}//상품 삭제 publicvoiddelete(intcode){try{Stringsql="delete from tbl_product where code=?";PreparedStatementps=Database.CON.prepareStatement(sql);ps.setInt(1,code);ps.execute();}catch(Exceptione){System.out.println("delete: "+e.toString());}}//상품등록publicvoidinsert(ProductVOvo){try{Stringsql="insert into tbl_product(title,price) values(?,?)";PreparedStatementps=Database.CON.prepareStatement(sql);ps.setString(1,vo.getTitle());ps.setInt(2,vo.getPrice());ps.execute();}catch(Exceptione){System.out.println("list : "+e.toString());}}//상품목록publicArrayList<ProductVO>list(){ArrayList<ProductVO>array=newArrayList<ProductVO>();try{Stringsql="select * from tbl_product order by code desc";PreparedStatementps=Database.CON.prepareStatement(sql);ResultSetrs=ps.executeQuery();while(rs.next()){ProductVOvo=newProductVO();vo.setCode(rs.getInt("code"));vo.setTitle(rs.getString("title"));vo.setPrice(rs.getInt("price"));System.out.println(vo.toString());array.add(vo);}}catch(Exceptione){System.out.println("list : "+e.toString());}returnarray;}}
WebContent/list.jsp
상품의 목록을 보여주는 페이지이다.
```jsp
<%@ page language=”java” contentType=”text/html; charset=UTF-8”
pageEncoding=”UTF-8”%>
<%@taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core”%>
<%@taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt”%>
<!DOCTYPE html>
</tr>
</c:forEach>
</table>
</body>
</html>
```
- 삭제 버튼을 누르면 상품번호 삭제도 가능하다. ( sql문으로 code 를 읽어서 삭제함)
![jsp상품목록]](/assets/images/jsp상품목록.JPG)
## WebContent/insert.jsp
- 상품을 등록하는 페이지이다. 이름과 가격을 받아서 목록에 보여준다. 상품번호는 auto_increment 로 자동으로 1씩증가한다.
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
상품등록
댓글남기기