728x90
먼저 이것을 만드는게 우선이지만
기능들을 작동하게 만드는 것이 궁금했다.
우선 상품을 입력받는 페이지를 만들어보자
7가지로 구성되어있다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상품 등록</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<%@ include file="menu.jsp" %>
<div class="jumbotron">
<div class="container">
<h1 class="display-3">상품 등록 </h1>
</div>
</div>
<div class="container">
<form name="newProduct" action="./process/processAddProduct.jsp"
class="form-horizontal" method="post">
<div class="form-group row">
<label class="col-sm-2">상품 코드</label>
<div class="col-sm-3">
<input type="text" name="productId" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">상품명</label>
<div class="col-sm-3">
<input type="text" name="name" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">가격</label>
<div class="col-sm-3">
<input type="text" name="unitPrice" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">상세 정보</label>
<div class="col-sm-3">
<textarea name="description" cols="50" rows="2"></textarea>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">제조사</label>
<div class="col-sm-3">
<input type="text" name="manufacturer" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">분류</label>
<div class="col-sm-3">
<input type="text" name="category" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">재고 수</label>
<div class="col-sm-3">
<input type="text" name="unitsInStock" class="form-control">
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" value="등록" >
</div>
</div>
</form>
</div>
<%@ include file="./footer/footer.jsp" %>
</body>
</html>
이러한 형태로 제작 하였으며,
입력한 값은 processAddProduct.jsp 으로 데이터가 넘어간다.
<%@page import="java.util.concurrent.Flow.Publisher"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="DTO.Product"%>
<%@ page import="DAO.ProductRepository"%>
<%@ page import="DAO.ProductRepository"%>
<%
request.setCharacterEncoding("utf-8");
String productId = request.getParameter("productId");
String name = request.getParameter("name");
Integer unitPrice = Integer.parseInt(request.getParameter("unitPrice"));
String description = request.getParameter("description");
String manufacturer = request.getParameter("manufacturer");
String category = request.getParameter("category");
Integer unitsInStock = Integer.parseInt(request.getParameter("unitsInStock"));
Product product = new Product(productId,name,unitPrice,description,manufacturer,category,unitsInStock);
ProductRepository.insertProduct(product);
response.sendRedirect("/WebMarketY/Web/Products.jsp");
%>
입력받은 값을 각각에 대입하여 저장하고
데이터 베이스에 넘긴다.
그리고 입력이 끝난 사용자의 페이지를
상품목록 페이지로 이동시켜 등록된 모습을 보여 주도록 하였다.
DAO를 보도록하자
package DAO;
import java.util.ArrayList;
import java.util.List;
import DTO.Product;
import common.JDBConnect;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class ProductRepository {
private ArrayList<Product> listOfProducts = new ArrayList<Product>();
private static ProductRepository instance = new ProductRepository();
public static ProductRepository getInstance() {
return instance;
}
// public ProductRepository() {
// Product phone = new Product("P1234", "iPhone 6s", 800000);
// phone.setDescription("4.17-inch, 1334X750 Renina HD display, 8-megapixel iSight Camera");
// phone.setCategory("Smart Phone");
// phone.setManufacturer("Apple");
// phone.setUnitsInStock(1000);
//
// Product notebook = new Product("P1235", "LG PC 그램", 1500000);
// notebook.setDescription("13.3-inch, IPS LED display, 5rd Generation Intel Core processors");
// notebook.setCategory("NoteBook");
// notebook.setManufacturer("LG");
// notebook.setUnitsInStock(1000);
//
// Product tablet = new Product("P1236", "Galaxy Tab S", 900000);
// tablet.setDescription("212.8*125.6*6.6mm, Super AMOLED display, Octa-Core Processor");
// tablet.setCategory("Tablet");
// tablet.setManufacturer("Samsung");
// tablet.setUnitsInStock(1000);
//
// listOfProducts.add(phone);
// listOfProducts.add(notebook);
// listOfProducts.add(tablet);
//
// }
public ArrayList<Product> getAllProducts(){
return listOfProducts;
}
public Product getProductById(String productId) {
Product productById = null;
for(int i=0; i<listOfProducts.size(); i++) {
Product product = listOfProducts.get(i);
if(product!=null && product.getProductId()!= null && product.getProductId().equals(productId)) {
productById = product;
break;
}
}
return productById;
}
public static List<Product> selctlistProduct() {
List<Product> productList = new ArrayList<>();
JDBConnect jdbc = new JDBConnect();
String sqlSelect = "select * from Product";
PreparedStatement psmt = null;
ResultSet rs = null;
try {
psmt = jdbc.con.prepareStatement(sqlSelect);
rs = psmt.executeQuery();
while (rs.next()) {
Product product = new Product(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4),
rs.getString(5), rs.getString(6), rs.getInt(7));
productList.add(product);
}
;
return productList;
} catch (Exception e) {
e.printStackTrace();
}
return productList;
}
public static List<Product> selctlistProductName(String name) {
List<Product> productList = new ArrayList<>();
JDBConnect jdbc = new JDBConnect();
String sqlSelect = "select * from Product WHERE productId = '"+ name +"'";
PreparedStatement psmt = null;
ResultSet rs = null;
try {
psmt = jdbc.con.prepareStatement(sqlSelect);
rs = psmt.executeQuery();
while (rs.next()) {
Product product = new Product(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getString(4),
rs.getString(5), rs.getString(6), rs.getInt(7));
productList.add(product);
}
;
return productList;
} catch (Exception e) {
e.printStackTrace();
}
return productList;
}
public static Product insertProduct(Product product1) {
Product product = null;
JDBConnect jdbc = new JDBConnect();
PreparedStatement psmt = null;
String sql = "INSERT INTO PRODUCT VALUES (?, ?, ?, ?, ?, ?, ?)";
try {
psmt = jdbc.con.prepareStatement(sql);
psmt.setString(1, product1.getProductId());
psmt.setString(2, product1.getPname());
psmt.setInt(3, product1.getUnitPrice());
psmt.setString(4, product1.getDescription());
psmt.setString(5, product1.getManufacturer());
psmt.setString(6, product1.getCategory());
psmt.setInt(7, product1.getUnitsInStock());
int a = psmt.executeUpdate();
if(a>0) {
System.out.println("와댓다신난다 ");
}else {
System.out.println("다시해야되노");
}
} catch (Exception e) {
e.printStackTrace();
}
return product;
}
public void addProduct(Product product) {
listOfProducts.add(product);
}
}
원래의 저값도 어레이에 등록해 두었지만 ( 주석 처리 하였다 )
이제 반복문으로 값을 불러와서 상품 페이지를 모두 보여주도록 하였다.
입력된 값을 등록하는 부분은 아래와 같다.
public static Product insertProduct(Product product1) {
Product product = null;
JDBConnect jdbc = new JDBConnect();
PreparedStatement psmt = null;
String sql = "INSERT INTO PRODUCT VALUES (?, ?, ?, ?, ?, ?, ?)";
try {
psmt = jdbc.con.prepareStatement(sql);
psmt.setString(1, product1.getProductId());
psmt.setString(2, product1.getPname());
psmt.setInt(3, product1.getUnitPrice());
psmt.setString(4, product1.getDescription());
psmt.setString(5, product1.getManufacturer());
psmt.setString(6, product1.getCategory());
psmt.setInt(7, product1.getUnitsInStock());
int a = psmt.executeUpdate();
if(a>0) {
System.out.println("와댓다신난다 ");
}else {
System.out.println("다시해야되노");
}
} catch (Exception e) {
e.printStackTrace();
}
return product;
}
위와 같이 차례대로 값을 받아 쿼리문을 작성 하게 되고
executeUpdate 메서드를 활용하여 쿼리문을 실행 시켜 데이터 베이스에 반영을 한다.
728x90
'국비교육 [完] > WebMarket' 카테고리의 다른 글
11. 게시판을 만들어 보자 (0) | 2023.03.09 |
---|---|
10. 로그아웃 (0) | 2023.03.09 |
8. 로그인이 안되어 있으면 로그인 창으로 (0) | 2023.03.09 |
7. 로그인 유지 기능 (0) | 2023.03.09 |
6. 세션으로 로그인 (0) | 2023.03.09 |