Skip to content
Permalink
Browse files
product update and delete
  • Loading branch information
Roshan Koshy Thomas committed Aug 25, 2022
1 parent 85c1e93 commit 47b06bc18f08dc5c1016118aacdf188914dad5e4
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 49 deletions.
@@ -1,6 +1,7 @@



<%@page import="cfy.model.User"%>
<%@page import="cfy.model.Product"%>
<%@page import="java.util.List"%>
<%
@@ -32,8 +33,7 @@ List<Product> prod = (List<Product>) request.getAttribute("product");

<div class="admin-product-form-container">

<form action="admin" method="post"
enctype="multipart/form-data">
<form action="admin" method="post" enctype="multipart/form-data">
<h3>add a new product</h3>
<input type="text" placeholder="enter product name"
name="product_name" class="box"> <input type="number"
@@ -76,9 +76,10 @@ List<Product> prod = (List<Product>) request.getAttribute("product");
<td><%=p.getDetails()%></td>
<td><%=p.getCategory()%></td>

<td><a href="" class="btn"> <i class="fas fa-edit"></i>
<td><a href="admin?action=update&id=<%=p.getId()%>" class="btn"> <i class="fas fa-edit"></i>
edit
</a> <a href="" class="btn"> <i class="fas fa-trash"></i> delete
</a> <a href="admin?action=del&id=<%=p.getId()%>" class="btn"> <i
class="fas fa-trash"></i> delete
</a></td>
</tr>

@@ -0,0 +1,63 @@



<%@page import="cfy.model.User"%>
<%@page import="cfy.model.Product"%>
<%@page import="java.util.List"%>
<%
Product prod = (Product) request.getAttribute("product");
%>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>admin update page</title>

<!-- font awesome cdn link -->
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

<!-- custom css file link -->
<link rel="stylesheet" href="./css/admin.css">

</head>
<body>



<div class="container">

<div class="admin-product-form-container">

<form action="admin?action=update" method="post">
<h3>Update product</h3>
<input type="text" value="<%=prod.getId()%>" name="product_id"
hidden>
<input type="text" placeholder="enter product name"
name="product_name" class="box" value="<%=prod.getName()%>">
<input type="number"
placeholder="enter product price" name="product_price" class="box"
value="<%=prod.getPrice()%>">
<input type="text"
placeholder="enter product details" name="product_details"
class="box" value="<%=prod.getDetails()%>">
<input
type="text" placeholder="enter product category"
name="product_category" class="box" value="<%=prod.getCategory()%>">
<input type="submit" class="btn" name="add_product"
value="update product">
</form>

</div>



</div>


</body>
</html>
@@ -19,7 +19,7 @@ public class ProductDao {
}

public List<Product> getAllProducts() {
List<Product> book = new ArrayList<>();
List<Product> prod = new ArrayList<>();
try {

query = "select * from products";
@@ -35,14 +35,14 @@ public class ProductDao {
row.setPrice(rs.getInt("price"));
row.setImage(rs.getString("image"));

book.add(row);
prod.add(row);
}

} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return book;
return prod;
}

public Product getSingleProduct(int id) {
@@ -142,4 +142,43 @@ public class ProductDao {
}
return result;
}

public int delete(int parseInt) {

int result = 0;

query = "delete from products where id=?";
try {

pst = this.con.prepareStatement(query);
pst.setInt(1, parseInt);

result = pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;
}

public int update(Product prod) {

int result = 0;
query = "update products set name=?,category=?,details=?,price=? where id=?";
try {

pst = this.con.prepareStatement(query);
pst.setString(1, prod.getName());
pst.setString(2, prod.getCategory());
pst.setString(3, prod.getDetails());
pst.setInt(4, prod.getPrice());
pst.setInt(5, prod.getId());
result = pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
@@ -26,6 +26,18 @@ public class Product {
}




public Product(int id, String name, String category, String details, int price) {
super();
this.id = id;
this.name = name;
this.category = category;
this.details = details;
this.price = price;
}


public int getId() {
return id;
}
@@ -18,6 +18,7 @@ import javax.servlet.http.Part;
import cfy.connection.DbCon;
import cfy.dao.ProductDao;
import cfy.model.Product;
import cfy.model.User;

/**
* Servlet implementation class AdminPanel
@@ -42,58 +43,97 @@ public class AdminPanel extends HttpServlet {
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("@amin");
List<Product> product = new ArrayList<>();
try {
product = new ProductDao(DbCon.getConnection()).getAllProducts();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

if ("del".equals(request.getParameter("action"))) {

if (request.getParameter("id") != null) {
try {
int status = new ProductDao(DbCon.getConnection())
.delete(Integer.parseInt(request.getParameter("id")));
} catch (NumberFormatException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

response.sendRedirect("admin");
} else if("update".equals(request.getParameter("action"))) {

Product product = null;
try {
product = new ProductDao(DbCon.getConnection()).getSingleProduct(Integer.parseInt(request.getParameter("id")));
} catch (NumberFormatException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

request.setAttribute("product", product);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/adminUpdate.jsp");
rd.forward(request, response);
}

else {
List<Product> product = new ArrayList<>();
try {
product = new ProductDao(DbCon.getConnection()).getAllProducts();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("product", product);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin.jsp");
rd.forward(request, response);
}
request.setAttribute("product", product);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin.jsp");
rd.forward(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

if ("update".equals(request.getParameter("action"))) {


try {
int result = new ProductDao(DbCon.getConnection()).update(new Product(
Integer.parseInt(request.getParameter("product_id")), request.getParameter("product_name"),
request.getParameter("product_category"), request.getParameter("product_details"),
Integer.parseInt(request.getParameter("product_price"))));
} catch (NumberFormatException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

response.sendRedirect("admin");

} else {
Part part = request.getPart("product_image");
String fn = part.getSubmittedFileName();
String imagepath = getServletContext().getRealPath("/product-image" + File.separator + fn);
log(imagepath);

File theDir = new File(imagepath);
if (!theDir.exists()) {
theDir.mkdirs();
}

String name = "";
int price = 0;
String detail = "";
String category = "";

Part part = request.getPart("product_image");
String fn = part.getSubmittedFileName();
String imagepath = getServletContext().getRealPath("/product-image" + File.separator + fn);
log(imagepath);

File theDir = new File(imagepath);
if (!theDir.exists()) {
theDir.mkdirs();
}
try {
part.write(imagepath);
Product prod = new Product();
prod.setName(request.getParameter("product_name"));
prod.setPrice(Integer.parseInt(request.getParameter("product_price")));
prod.setCategory(request.getParameter("product_category"));
prod.setDetails(request.getParameter("product_details"));
prod.setImage(fn);

try {
part.write(imagepath);
Product prod = new Product();
prod.setName(request.getParameter("product_name"));
prod.setPrice(Integer.parseInt(request.getParameter("product_price")));
prod.setCategory(request.getParameter("product_category"));
prod.setDetails(request.getParameter("product_details"));
prod.setImage(fn);
int res = new ProductDao(DbCon.getConnection()).add(prod);

int res = new ProductDao(DbCon.getConnection()).add(prod);
if (res > 0) {
response.sendRedirect("admin");
}

if (res > 0) {
response.sendRedirect("admin");
} catch (Exception e) {
// TODO: handle exception
}

} catch (Exception e) {
// TODO: handle exception
}

}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 47b06bc

Please sign in to comment.