0% found this document useful (0 votes)
32 views4 pages

Add Product SOURCE

The document contains code for an e-commerce product management application using Java and Hibernate. It includes a Product class with product details, a DbUtil class to connect to the database, a ProductDAO class to add products to the database, and JSP files to display a product entry form, add products by calling the DAO, and confirm success or failure.

Uploaded by

Sourav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Add Product SOURCE

The document contains code for an e-commerce product management application using Java and Hibernate. It includes a Product class with product details, a DbUtil class to connect to the database, a ProductDAO class to add products to the database, and JSP files to display a product entry form, add products by calling the DAO, and confirm success or failure.

Uploaded by

Sourav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Source Code

Class- Product
package com.ecom.pojo;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Product {
@Id

private int pid;


private String pname;
private float cost;
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public float getCost() {
return cost;
}
public void setCost(float cost) {
this.cost = cost;
}

}
Class- DbUtil
package com.ecom.dbutil;
//dbutil

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class DbUtil {

StandardServiceRegistry ssr=null;
Metadata md=null;
SessionFactory sf=null;
Session session=null;

public Session dbConn() {


//SSR is used to map the config file and execute it .
ssr=new
StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
//Metadata of the xml file is read by this object
md=new MetadataSources(ssr).getMetadataBuilder().build();
//session-factory- db
sf=md.getSessionFactoryBuilder().build();
//all the crud operations need to be done in Session
session=sf.openSession();

return session;
}

Class- ProductDAO

package com.prodcut.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.ecom.dbutil.*;
import com.ecom.pojo.Product;

public class ProductDAO {


public int addProduct(Product product) {
DbUtil dbconn=new DbUtil();
Session session=dbconn.dbConn();
Transaction trans=session.beginTransaction();
int value=(Integer) session.save(product);
trans.commit();
session.close();
return value;

}
}

1) Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1><i>Add a product</i></h1>
<form action="addController.jsp">
<table>
<tr><td>Product Id</td><td><input type="text" name="pid"></td></tr>
<tr><td>Product Name</td><td><input type="text" name="pname"></td></tr>
<tr><td>Product Cost</td><td><input type="text" name="pcost"></td></tr>
<tr><td></td><td><input type="submit" value="add"></td></tr>
</table>

</form>

</body>
</html>

2) addController.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.prodcut.dao.*" %>
<%@ page import="com.ecom.pojo.*" %>
<%@page import="java.util.*" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ProductDAO dao=new ProductDAO();
Product product=new Product();
product.setPid(Integer.parseInt(request.getParameter("pid")));
product.setPname(request.getParameter("pname"));
product.setCost(Float.parseFloat(request.getParameter("pcost")));
//mysql always accept the date in the format of yyyy-MM-dd
//convert the java based util date to sql date

int row=dao.addProduct(product);
if(row>0){
response.sendRedirect("success.jsp");
}
else{
response.sendRedirect("fail.jsp");
}

%>

</body>
</html>

3) success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>the product got added successfully....</h1>
<%@ include file="index.jsp" %>
</body>
</html>

You might also like