Add Product SOURCE
Add Product SOURCE
Class- Product
package com.ecom.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Product {
@Id
}
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;
StandardServiceRegistry ssr=null;
Metadata md=null;
SessionFactory sf=null;
Session session=null;
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;
}
}
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