create table product(
pnum number primary key,
pname varchar2(50) not null,
pcategory_fk varchar2(20) not null,
pcompany varchar2(50),
pimage varchar2(30),
pqty number,
price number,
pspec varchar2(12),
pcontents varchar2(300),
point number,
pinputdate varchar2(10)
);
create sequence prod_seq
start with 1
increment by 1;
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.util.*, my.shop.*" %>
<!-- prod_input.jsp -->
<%@ include file="top.jsp" %>
<jsp:useBean id="cdao" class="my.shop.CategoryDAO"/>
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="cdao" property="pool" value="<%=pool%>"/>
<% List<CategoryDTO> list = cdao.listCate();
if (list == null || list.size() == 0){%>
<script type="text/javascript">
alert("카테고리를 먼저 등록해 주셔야 합니다.")
location.href="main.jsp"
</script>
<% return;
}%>
<div align="center">
<form name="f" action="prod_input_ok.jsp"
method="post" enctype="multipart/form-data">
<table border="0" class="outline" width="60%">
<caption>상품등록카테고리</caption>
<tr>
<th class="m2">카테고리</th>
<td>
<select name="pcategory_fk">
<% for(CategoryDTO dto : list){ %>
<option value="<%=dto.getCode()%>">
<%=dto.getCname()%>[<%=dto.getCode()%>]
</option>
<% } %>
</select>
</td>
</tr>
<tr>
<th class="m2">상품명</th>
<td><input type="text" name="pname" class="box"></td>
</tr>
<tr>
<th class="m2">상품코드</th>
<td><input type="text" name="pcode" class="box"></td>
</tr>
<tr>
<th class="m2">제조회사</th>
<td><input type="text" name="pcompany" class="box"></td>
</tr>
<tr>
<th class="m2">상품이미지</th>
<td><input type="file" name="pimage" class="box"></td>
</tr>
<tr>
<th class="m2">상품수량</th>
<td><input type="text" name="pqty" class="box"></td>
</tr>
<tr>
<th class="m2">상품가격</th>
<td><input type="text" name="price" class="box"></td>
</tr>
<tr>
<th class="m2">상품스팩</th>
<td>
<select name="pspec">
<option value="normal">NORMAL</option>
<option value="hit">HIT</option>
<option value="new">NEW</option>
<option value="best">BEST</option>
</select>
</td>
</tr>
<tr>
<th class="m2">상품소개</th>
<td>
<textarea name="pcontents" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<th class="m2">상품포인트</th>
<td><input type="text" name="point" class="box"></td>
</tr>
<tr>
<td align="center">
<input type="submit" value="상품등록">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</div>
<%@ include file="bottom.jsp"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="com.oreilly.servlet.*, java.io.*"%>
<!-- prod_input_ok.jsp -->
<jsp:useBean id="pdao" class="my.shop.ProductDAO" />
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="pdao" property="pool" value="<%=pool%>"/>
<%
MultipartRequest mr = null;
String upPath = config.getServletContext().getRealPath("/myshop/images");
int len = 20*1024*1024;
try{
mr = new MultipartRequest(request, upPath, len, "EUC-KR");
}catch(IOException e){
e.printStackTrace();%>
<script type="text/javascript">
alert("파일 업로드 중 오류 발생!! 관리자에게 문의하세요")
location.href="main.jsp"
</script>
<% return;
}
int res = pdao.insertProduct(mr);
if (res>0){%>
<script type="text/javascript">
alert("상품등록 성공!! 상품목록페이지로 이동합니다.")
location.href="prod_list.jsp";
</script>
<% }else { %>
<script type="text/javascript">
alert("상품등록 실패!! 상품등록페이지로 이동합니다.")
location.href="prod_input.jsp";
</script>
<% } %>
// makeList
protected List<ProductDTO> makeList(ResultSet rs) throws SQLException {
List<ProductDTO> list = new ArrayList<>();
while(rs.next()) {
ProductDTO dto = new ProductDTO();
dto.setPnum(rs.getInt("pnum"));
dto.setPname(rs.getString("pname"));
dto.setPcategory_fk(rs.getString("pcategory_fk"));
dto.setPcompany(rs.getString("pcompany"));
dto.setPimage(rs.getString("pimage"));
dto.setPqty(rs.getInt("pqty"));
dto.setPrice(rs.getInt("price"));
dto.setPspec(rs.getString("pspec"));
dto.setPcontents(rs.getString("pcontents"));
dto.setPoint(rs.getInt("point"));
dto.setPinputdate(rs.getString("pinputdate"));
list.add(dto);
}
return list;
}
// 상품 추가 메서드
public int insertProduct(MultipartRequest mr) throws SQLException {
try {
con = pool.getConnection();
String sql = "insert into product values"
+ "(prod_seq.nextval,?,?,?,?,?,?,?,?,?,sysdate)";
ps = con.prepareStatement(sql);
ps.setString(1, mr.getParameter("pname"));
String pcategory_fk = mr.getParameter("pcategory_fk");
pcategory_fk += mr.getParameter("pcode");
ps.setString(2, pcategory_fk);
ps.setString(3, mr.getParameter("pcompany"));
ps.setString(4, mr.getFilesystemName("pimage"));
ps.setInt(5, Integer.parseInt(mr.getParameter("pqty")));
ps.setInt(6, Integer.parseInt(mr.getParameter("price")));
ps.setString(7, mr.getParameter("pspec"));
ps.setString(8, mr.getParameter("pcontents"));
ps.setInt(9, Integer.parseInt(mr.getParameter("point")));
int res = ps.executeUpdate();
return res;
}finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
이미지 등록은 input 태그 type = "file" 로 하고, 객체를 보낼 때는 더이상 일반 DTO 객체로 보내면 안 되고 MultipartRequest 로 보내야 한다.
그래서 이미지가 저장되어 있는 곳까지
String upPath = config.getServletContext().getRealPath("/myshop/images");
로 정해주는 것도 중요하다.
multipartRequest는 무조건 try catch 구문으로 감싸야 한다.
DAO 를 보면 받을 때도 MultipartRequest mr 로 받는 걸 알 수 있다.
cos.jar 파일 다운로드 받아야 함
(인터넷에서 다운받으면 된다 ^^)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<% request.setCharacterEncoding("EUC-KR"); %>
<%@ page import="java.util.*, my.shop.*" %>
<!-- prod_list.jsp -->
<jsp:useBean id="pdao" class="my.shop.ProductDAO" />
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="pdao" property="pool" value="<%=pool%>"/>
<%@ include file="top.jsp" %>
<script type="text/javascript">
function checkDel(pnum, pimage){
var isDel = window.confirm("정말로 삭제하시겠습니까?")
if (isDel){
document.del.pnum.value = pnum
document.del.pimage.value = pimage
document.del.submit()
}
}
</script>
<div align="center">
<table border="0" width="80%" class="outline">
<caption>상품목록</caption>
<tr class="m2">
<th>번호</th>
<th>상품코드</th>
<th>상품명</th>
<th>이미지</th>
<th>가격</th>
<th>제조사</th>
<th>수량</th>
<th>수정|삭제</th>
</tr>
<% List<ProductDTO> list = pdao.listProduct();
if (list == null || list.size() == 0){%>
<tr>
<td colspan="7">등록된 상품이 없습니다.</td>
</tr>
<% }else {
String upPath = request.getServletContext().getRealPath("/myshop/images");
java.text.DecimalFormat df = new java.text.DecimalFormat("###,###");
for(ProductDTO dto : list){%>
<tr>
<td><%=dto.getPnum()%></td>
<td><%=dto.getPcategory_fk()%></td>
<td><%=dto.getPname()%></td>
<td>
<a href="prod_view.jsp?pnum=<%=dto.getPnum()%>">
<img src="<%=upPath%>/<%=dto.getPimage()%>" width="40" height="40">
</a>
</td>
<td align="right"><%=df.format(dto.getPrice())%>원</td>
<td><%=dto.getPcompany()%></td>
<td align="right"><%=dto.getPqty()%>ea</td>
<td>
<a href="prod_update.jsp?pnum=<%=dto.getPnum()%>">수정</a> |
<a href="javascript:checkDel('<%=dto.getPnum()%>','<%=dto.getPimage()%>')">삭제</a>
</td>
</tr>
<% }
}%>
</table>
</div>
<form name="del" action="prod_delete.jsp" method="post">
<input type="hidden" name="pnum">
<input type="hidden" name="pimage">
</form>
<%@ include file="bottom.jsp"%>
public List<ProductDTO> listProduct() throws SQLException {
try {
con = pool.getConnection();
String sql = "select * from product";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
List<ProductDTO> list = makeList(rs);
return list;
}finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
public ProductDTO getProduct(int pnum) throws SQLException {
try {
con = pool.getConnection();
String sql = "select * from product where pnum = ?";
ps = con.prepareStatement(sql);
ps.setInt(1, pnum);
rs = ps.executeQuery();
List<ProductDTO> list = makeList(rs);
return list.get(0);
}finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.io.*"%>
<!-- prod_delete.jsp -->
<jsp:useBean id="pdao" class="my.shop.ProductDAO" />
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="pdao" property="pool" value="<%=pool%>"/>
<%
String pnum = request.getParameter("pnum");
String pimage = request.getParameter("pimage");
if (pnum == null || pnum.trim().equals("") ||
pimage == null || pimage.trim().equals("")){
response.sendRedirect("prod_list.jsp");
return;
}
int res = pdao.deleteProduct(Integer.parseInt(pnum));
String msg = null, url = "prod_list.jsp";
if (res > 0){
String upPath = config.getServletContext().getRealPath("/myshop/images");
File file = new File(upPath, pimage);
if (file.exists()){
file.delete();
msg = "상품삭제 성공(이미지도 삭제 성공)!! 상품목록페이지로 이동합니다.";
}else {
msg = "상품삭제 성공(이미지도 삭제 실패)!! 상품목록페이지로 이동합니다.";
}
}else {
msg = "상품삭제 실패!! 상품목록페이지로 이동합니다.";
}
%>
<script type="text/javascript">
alert("<%=msg%>")
location.href="<%=url%>"
</script>
public int deleteProduct(int pnum) throws SQLException {
try {
con = pool.getConnection();
String sql = "delete from product where pnum = ?";
ps = con.prepareStatement(sql);
ps.setInt(1, pnum);
int res = ps.executeUpdate();
return res;
}finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}
⭐ 중요한 점 : DB 에 저장된 값 그대로 불러올 것
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="my.shop.*"%>
<!-- prod_update.jsp -->
<jsp:useBean id="pdao" class="my.shop.ProductDAO" />
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="pdao" property="pool" value="<%=pool%>"/>
<%
String pnum = request.getParameter("pnum");
if (pnum == null || pnum.trim().equals("")){
response.sendRedirect("prod_list.jsp");
return;
}
ProductDTO dto = pdao.getProduct(Integer.parseInt(pnum));
String upPath = request.getServletContext().getRealPath("/myshop/images");
String[] spec = new String[]{"normal", "hit", "new", "best"};
%>
<%@ include file="top.jsp" %>
<div align="center">
<form name="f" action="prod_update_ok.jsp"
method="post" enctype="multipart/form-data">
<table border="0" class="outline" width="60%">
<caption>상품수정</caption>
<tr>
<th class="m2">카테고리</th>
<td>
<input type="text" name="pcategory_fk"
value="<%=dto.getPcategory_fk()%>" readOnly>
</td>
</tr>
<tr>
<th class="m2">상품번호</th>
<td>
<%=dto.getPnum()%>
<input type="hidden" name="pnum" value="<%=dto.getPnum()%>">
</td>
</tr>
<tr>
<th class="m2">상품명</th>
<td><input type="text" name="pname" class="box" value="<%=dto.getPname()%>"></td>
</tr>
<tr>
<th class="m2">제조회사</th>
<td><input type="text" name="pcompany" class="box" value="<%=dto.getPcompany()%>"></td>
</tr>
<tr>
<th class="m2">상품이미지</th>
<td>
<img src="<%=upPath%>/<%=dto.getPimage()%>" width="80" height="80">
<input type="file" name="pimage" class="box">
<input type="hidden" name="pimage2" value="<%=dto.getPimage()%>"/>
</td>
</tr>
<tr>
<th class="m2">상품수량</th>
<td><input type="text" name="pqty" class="box" value="<%=dto.getPqty()%>"></td>
</tr>
<tr>
<th class="m2">상품가격</th>
<td><input type="text" name="price" class="box" value="<%=dto.getPrice()%>"></td>
</tr>
<tr>
<th class="m2">상품스팩</th>
<td>
<select name="pspec">
<% for(String s : spec){
if (dto.getPspec().equals(s)){%>
<option value="<%=s%>" selected><%=s.toUpperCase()%></option>
<% }else {%>
<option value="<%=s%>"><%=s.toUpperCase()%></option>
<% }
}%>
</select>
</td>
</tr>
<tr>
<th class="m2">상품소개</th>
<td>
<textarea name="pcontents" rows="5" cols="30"><%=dto.getPcontents()%></textarea>
</td>
</tr>
<tr>
<th class="m2">상품포인트</th>
<td><input type="text" name="point" class="box" value="<%=dto.getPoint()%>"></td>
</tr>
<tr>
<td align="center">
<input type="submit" value="상품수정">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</div>
<%@ include file="bottom.jsp"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="com.oreilly.servlet.*, java.io.*"%>
<!-- prod_update_ok.jsp -->
<jsp:useBean id="pdao" class="my.shop.ProductDAO" />
<jsp:useBean id="pool" class="my.db.ConnectionPoolBean" scope="application"/>
<jsp:setProperty name="pdao" property="pool" value="<%=pool%>"/>
<%
MultipartRequest mr = null;
String upPath = config.getServletContext().getRealPath("/myshop/images");
int len = 20*1024*1024;
try{
mr = new MultipartRequest(request, upPath, len, "EUC-KR");
}catch(IOException e){
e.printStackTrace();%>
<script type="text/javascript">
alert("파일 업로드 중 오류 발생!! 관리자에게 문의하세요")
location.href="main.jsp"
</script>
<% return;
}
int res = pdao.updateProduct(mr);
if (res>0){%>
<script type="text/javascript">
alert("상품수정 성공!! 상품목록페이지로 이동합니다.")
location.href="prod_list.jsp";
</script>
<% }else { %>
<script type="text/javascript">
alert("상품수정 실패!! 상품수정페이지로 이동합니다.")
location.href="prod_update.jsp?pnum="<%=mr.getParameter("pnum")%>;
</script>
<% } %>
public int updateProduct(MultipartRequest mr) throws SQLException {
try {
con = pool.getConnection();
String sql = "update product set pname=?, pcompany=?, pimage=?, "
+ "pqty=?, price=?, pspec=?, pcontents=?, point=? where pnum=?";
ps = con.prepareStatement(sql);
ps.setString(1, mr.getParameter("pname"));
ps.setString(2, mr.getParameter("pcompany"));
String pimage = mr.getFilesystemName("pimage");
if (pimage == null) pimage = mr.getParameter("pimage2");
ps.setString(3, pimage);
ps.setInt(4, Integer.parseInt(mr.getParameter("pqty")));
ps.setInt(5, Integer.parseInt(mr.getParameter("price")));
ps.setString(6, mr.getParameter("pspec"));
ps.setString(7, mr.getParameter("pcontents"));
ps.setInt(8, Integer.parseInt(mr.getParameter("point")));
ps.setInt(9, Integer.parseInt(mr.getParameter("pnum")));
int res = ps.executeUpdate();
return res;
}finally {
if (ps != null) ps.close();
if (con != null) pool.returnConnection(con);
}
}