본문 바로가기
JSP Servlet

물품관리 MVC1

by noddu 2021. 2. 8.
728x90
반응형

MVC1 패턴

 

Controller(비지니스 로직영역)에 View를 같이 구현하는 방식 ( 요청을 JSP가 전부처리함)

  빠르고 쉽지만 JSP하나에서 모두 이루어져서 유지보수에 문제가 발생한다.

 

▲이클립스에서 Model 1방식


-DataBase-

 

--상품--
create table proiot(
pnum number primary key,
pname varchar2(20) not null,
price number(10),
pemail varchar2(50),
ptel varchar2(20)
);
create sequence pnum_seq;
insert into PROIOT
values(pnum_seq.nextval, 'outer','20000','adidas@ad.com','010-2402-7267');

select * from proiot;
drop table proiot cascade constraint;

-Model-

●ProductDAO.java(interface)

package pro.model;

import java.util.List;

public interface ProductDAO {
	public List<ProductVO> productList(); //전체 상품
	public int productInsert(ProductVO vo); //상품추가
	public int productDelete(int pnum); //상품삭제
	public ProductVO productContent(int pnum); //상품상세
	public int productUpdate(ProductVO vo); //상품수정
}

 

●ProductDAOImpl.java

package pro.model;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ProductDAOImpl implements ProductDAO{
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	//CRUD 하기 DBconnect
	
	public void getConnect() {
		String url="jdbc:oracle:thin:@192.168.35.63:1521:xe";
		String user="system";
		String password="3309";
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn=DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}//DB close
	public void dbClose() {
		try {
			if(rs!=null) rs.close();
			if(ps!=null) ps.close();
			if(conn!=null) ps.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public List<ProductVO> productList() {
		//VO(묶고)->List(담고)
		String SQL="select * from proiot";
		List<ProductVO>list = new ArrayList<ProductVO>();
		getConnect();
		try {
			ps=conn.prepareStatement(SQL);
			rs=ps.executeQuery(); //rs = 커서 , 이동=next()
			while(rs.next()) {
				int pnum = rs.getInt("pnum");
				String pname = rs.getString("pname");
				int price = rs.getInt("price");
				String pemail = rs.getString("pemail");
				String ptel = rs.getString("ptel");
				ProductVO vo = new ProductVO(pnum, pname, price, pemail, ptel);
				list.add(vo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			dbClose();
		}
		return list;
	}
	@Override
	public int productInsert(ProductVO vo) {
		//상품추가
		String SQL ="insert into proiot values(pnum_seq.nextval,?,?,?,?)";
		getConnect();
		int cnt=-1; // -1 = 실패
		try {
			ps=conn.prepareStatement(SQL);
			ps.setString(1, vo.getPname());
			ps.setInt(2, vo.getPrice());
			ps.setString(3, vo.getPemail());
			ps.setString(4, vo.getPtel());
			cnt=ps.executeUpdate(); //실행
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			dbClose();
		}
		return cnt;
	}
	@Override
	public int productDelete(int pnum) {
		//상품삭제
		String SQL="delete from proiot where pnum = ?";
		getConnect();
		int cnt=-1;
		try {
			ps=conn.prepareStatement(SQL);
			ps.setInt(1, pnum);
			cnt=ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			dbClose();
		}
		return cnt;
	}
	@Override
	public ProductVO productContent(int pnum) {
		//상품상세
		String SQL="select * from proiot where pnum=?";
		getConnect();
		ProductVO vo =null;
		try {
			ps=conn.prepareStatement(SQL);
			ps.setInt(1, pnum);
			rs=ps.executeQuery();
			if(rs.next()) {
				pnum=rs.getInt("pnum");
				String pname = rs.getString("pname");
				int price = rs.getInt("price");
				String pemail = rs.getString("pemail");
				String ptel = rs.getString("ptel");
				vo=new ProductVO(pnum, pname, price, pemail, ptel);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			dbClose();
		}
		return vo;
	}
	@Override
	public int productUpdate(ProductVO vo) {
		//상품수정
		int cnt = -1; //실패의 의미로 -1을사용!
		String SQL = "update proiot set price=?, pemail=?, ptel=? where pnum=?";
		getConnect();
		try {
			ps = conn.prepareStatement(SQL);
			ps.setInt(1, vo.getPrice());
			ps.setString(2, vo.getPemail());
			ps.setString(3, vo.getPtel());
			ps.setInt(4, vo.getPnum());
			cnt = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			dbClose();
		}
		return cnt;
	}
	
	
}

 

●ProductVO.java

package pro.model;

public class ProductVO {
	private int pnum;
	private String pname;
	private int price;
	private String pemail;
	private String ptel;
	
	public ProductVO(int pnum, String pname, int price, String pemail, String ptel) {
		super();
		this.pnum = pnum;
		this.pname = pname;
		this.price = price;
		this.pemail = pemail;
		this.ptel = ptel;
	}
	public ProductVO(String pname, int price, String pemail, String ptel) {
		super();
		this.pname = pname;
		this.price = price;
		this.pemail = pemail;
		this.ptel = ptel;
	}
	public ProductVO(String pname, String pemail, String ptel) {
		super();
		this.pname = pname;
		this.pemail = pemail;
		this.ptel = ptel;
	}
	
	public ProductVO() { 	}

	public int getPnum() {
		return pnum;
	}

	public void setPnum(int pnum) {
		this.pnum = pnum;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getPemail() {
		return pemail;
	}

	public void setPemail(String pemail) {
		this.pemail = pemail;
	}

	public String getPtel() {
		return ptel;
	}

	public void setPtel(String ptel) {
		this.ptel = ptel;
	}

	
}


-View-

●productList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@page import="pro.model.*" %>
<%
	String cpath = request.getContextPath();
	ProductDAO dao=new ProductDAOImpl();
	List<ProductVO> list=dao.productList();
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<title>Insert title here</title>
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
<script type="text/javascript">
function insertFn(){
	 alert("OK");
	 location.href="productInsertForm.jsp";
  }
  function deleteFn(pnum){
	  //alert(pnum);
	  location.href="productDelete.jsp?pnum="+pnum;
  }
</script>
</head>
<body>
싸다 싸!! ★중고나라★(ver.1)
<table class="tg" style="undefined;table-layout: fixed; width: 680px">
<colgroup>
<col style="width: 102px">
<col style="width: 148px">
<col style="width: 114px">
<col style="width: 155px">
<col style="width: 161px">
<col style="width: 87px">
</colgroup>
<thead>
  <tr>
    <th class="tg-0lax">번호</th>
    <th class="tg-0lax">상품이름</th>
    <th class="tg-0lax">가격</th>
    <th class="tg-0lax">판매자 이메일</th>
    <th class="tg-0lax">전화번호</th>
    <th class="tg-0lax">삭제</th>
  </tr>
</thead>
<tbody>
  <% for(int i=0; i<list.size(); i++){
	ProductVO vo = list.get(i);%>
	<tr>
	<td><%=vo.getPnum()%></td>
	<td><a href="productContent.jsp?pnum=<%=vo.getPnum()%>"><%=vo.getPname() %></a></td>
	<td><%=vo.getPrice() %></td>
	<td><%=vo.getPemail() %></td>
	<td><%=vo.getPtel() %></td>
	<td class="tg-0pky"><input type="button" onclick="deleteFn(<%=vo.getPnum()%>)" class="btn btn-danger" value="삭제"/></td>
	</tr>
<% }%>
	<tr>
		<td class="tg-lqy6" colspan="6"><input type="button" class="btn btn-primary" onclick="insertFn()" value="상품추가"></td>
	</tr>
</tbody>
</table>
</body>
</html>

●productInsertFrom.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
  <%
   String cpath=request.getContextPath();
   %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-gyu3{border-color:inherit;font-family:Arial, Helvetica, sans-serif !important;;font-size:14px;text-align:left;
  vertical-align:top}
.tg .tg-3a3g{font-family:Arial, Helvetica, sans-serif !important;;font-size:14px;text-align:center;vertical-align:top}
.tg .tg-7vsa{font-family:Arial, Helvetica, sans-serif !important;;font-size:14px;text-align:left;vertical-align:top}
.tg .tg-jwph{background-color:#ffffff;border-color:inherit;color:#013300;font-family:Arial, Helvetica, sans-serif !important;;
  font-size:14px;text-align:center;vertical-align:top}
.tg .tg-3jcj{border-color:inherit;font-family:Arial, Helvetica, sans-serif !important;;font-size:14px;text-align:center;
  vertical-align:top}
</style>
</head>
<body>
<form action="productInsert.jsp" method="post">
   <table class="tg">
<tbody>
  <tr>
    <td class="tg-3jcj">상품이름</td>
    <td class="tg-gyu3"><input type="text" name="pname"></td>
  </tr>
  <tr>
    <td class="tg-3a3g">가격</td>
    <td class="tg-7vsa"><input type="text" name="price"></td>
  </tr>
  <tr>
    <td class="tg-3a3g">이메일</td>
    <td class="tg-7vsa"><input type="text" name="pemail"></td>
  </tr>
  <tr>
    <td class="tg-3a3g">전화번호</td>
    <td class="tg-7vsa"><input type="text" name="ptel"></td>
  </tr>
  <tr>
    <td class="tg-3a3g" colspan="2">
    	<input type="submit" value="상품추가">
    	<input type="reset" value="취소">
    </td>
  </tr>
</tbody>
</table>
</form>
</body>
</html>

●productInsert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="pro.model.*" %>
    <%
    String cpath=request.getContextPath();
    
    request.setCharacterEncoding("utf-8");
    String pname = request.getParameter("pname");
    int price =Integer.parseInt(request.getParameter("price"));
    String pemail = request.getParameter("pemail");
    String ptel = request.getParameter("ptel");
    
    ProductVO vo = new ProductVO(pname, price, pemail, ptel);
    ProductDAO dao = new ProductDAOImpl();
    
    int cnt = dao.productInsert(vo);
    if(cnt>0){
    	response.sendRedirect("productList.jsp");
    }else {
    	throw new ServletException("error");
    }
    %>

 

●productDelete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import ="pro.model.*" %>
<%
String cpath = request.getContextPath();
int pnum = Integer.parseInt(request.getParameter("pnum"));
ProductDAO dao = new ProductDAOImpl();
int cnt = dao.productDelete(pnum);
if(cnt>0) {
	response.sendRedirect("productList.jsp");
}else{
	throw new ServletException("error");
}
%>

 

●productContent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "pro.model.*" %>
<%
int pnum = Integer.parseInt(request.getParameter("pnum"));
	ProductDAO dao = new ProductDAOImpl();
	ProductVO vo = dao.productContent(pnum);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-baqh{text-align:center;vertical-align:top}
.tg .tg-fs2w{background-color:#cbcefb;font-weight:bold;text-align:center;vertical-align:top}
.tg .tg-0lax{text-align:left;vertical-align:top}
</style>
</head>
<body>

<%=vo.getPnum() %>번<%=vo.getPname() %>상품 상세보기
<form action="/PRO01/product/productUpdate.jsp" method="post">
<input type="hidden" name="pnum" value="<%=vo.getPnum()%>"/>
<table class="tg" style="undefined;table-layout: fixed; width: 552px">
<colgroup>
<col style="width: 107px">
<col style="width: 445px">
</colgroup>
<thead>
  <tr>
    <th class="tg-fs2w">상품번호</th>
    <th class="tg-0lax"><%=vo.getPnum()%></th>
  </tr>
</thead>
<tbody>
  <tr>
    <td class="tg-fs2w">상품명</td>
    <td class="tg-0lax"><%=vo.getPname()%></td>
  </tr>
  <tr>
  <tr>
    <td class="tg-fs2w">가격</td>
    <td class="tg-0lax"><input type="text" name="price" value="<%=vo.getPrice()%>"/></td>
  </tr>
  <tr>
    <td class="tg-fs2w">판매자 이메일</td>
    <td class="tg-0lax"><input type="text" name="pemail" value="<%=vo.getPemail()%>"/></td>
  </tr>
  <tr>
    <td class="tg-fs2w">전화번호</td>
    <td class="tg-0lax"><input type="text" name="ptel" value="<%=vo.getPtel()%>"/></td>
  </tr>
  <tr>
    <td class="tg-baqh" colspan="2">
      <input type="submit" value="수정하기"/>
      <input type="reset" value="취소"/>
      <input type="button" onclick="location.href='/PRO01/product/productList.jsp'" value="리스트"/>    
    </td>
  </tr>
</tbody>
</table>
</form>
</body>
</html>

●productUpdate.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import ="pro.model.*" %>
    
    <%
    
    request.setCharacterEncoding("utf-8");
    
    int pnum = Integer.parseInt(request.getParameter("pnum"));
    int price = Integer.parseInt(request.getParameter("price"));
    String pemail = request.getParameter("pemail");
    String ptel = request.getParameter("ptel");
    
    ProductVO vo = new ProductVO();
    vo.setPnum(pnum);
    vo.setPrice(price);
    vo.setPemail(pemail);
    vo.setPtel(ptel);
    
    ProductDAO dao = new ProductDAOImpl();
    int cnt = dao.productUpdate(vo);
    
    if(cnt>0){
    	response.sendRedirect("productList.jsp");
    }else{
    	throw new ServletException("error");
    }

    %>

 

반응형

'JSP Servlet' 카테고리의 다른 글

비동기식페이지(JQuery,Ajax,JSON)  (0) 2021.02.08
회원관리 MVC2+HandlerMapping+JSTL  (0) 2021.02.08
회원관리 MVC2(forward+FrontController)  (0) 2021.02.08
회원관리 MVC2(forward)  (0) 2021.02.08
JSP/Servlet  (0) 2021.02.08