0% found this document useful (0 votes)
37 views9 pages

Adding Products to SQL Database

The document describes how to add a new product to a database using Hibernate. It includes the EProduct entity class with fields for id, name, and price. It also includes servlets for adding and listing products, a Hibernate utility class to build the session factory, and HTML pages for the add product form and homepage.

Uploaded by

sagar salanke
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)
37 views9 pages

Adding Products to SQL Database

The document describes how to add a new product to a database using Hibernate. It includes the EProduct entity class with fields for id, name, and price. It also includes servlets for adding and listing products, a Hibernate utility class to build the session factory, and HTML pages for the add product form and homepage.

Uploaded by

sagar salanke
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

Adding a New Product in the Database

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@Entity
@Table(name="eproduct_10032022")
public class EProduct {

@Id
@GeneratedValue
@Column(name="employee_id")
private int id;

@Column(name="eproduct_name")
private String name;

@Column(name="eproduct_price")
private Double price;

// ALT +S+R
public int getId() {
return id;
}

public void setId(int id) {


[Link] = id;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}
public Double getPrice() {
return price;
}

public void setPrice(Double price) {


[Link] = price;
}

Servlet: AddProductServlet
package [Link];

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

/**
* Servlet implementation class AddProductServlet
*/
@WebServlet("/add-product")
public class AddProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public AddProductServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

[Link]("[Link]").include(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter pw = [Link]();

[Link]("<html><body>");

String name = [Link]("name");


String price = [Link]("price");

// STEP 1: Gets SessionFactory Object


SessionFactory sf = [Link]();

// STEP 2: Open Sesson


Session session = [Link]();

Transaction tx = [Link]();
EProduct ep = new EProduct();
[Link](name);
[Link]([Link](price));

[Link](ep);

[Link]();

[Link]("<h3 style=color:green'> Product is created successfully ! </h3>");

[Link]();

}
}

Servlet: ListProductServlet
package [Link];

import [Link];
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

import [Link];
import [Link];

import [Link];
import [Link];

/**
* Servlet implementation class ListProductServlet
*/
@WebServlet("/read-product")
public class ListProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public ListProductServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = [Link]();


[Link]("<html><body>");
SessionFactory sf = [Link]();
Session session = [Link]();

/*
* HQL - Hibernate Query Language. This is very similar to SQL language, only
* difference is Instead of Table name you need to refer EntityName
*
*/
List<EProduct> products = [Link](" from EProduct").list();
[Link]("<h1> Product List :- </h1>");
[Link]("<style> table,td,th {border:1px solid green;
padding:10px;}</style>");
[Link]("<table>");

[Link]("<tr>");
[Link]("<th> Product ID </th>");
[Link]("<th> Product Name </th>");
[Link]("<th> Product Price </th>");
[Link]("</tr>");

for (EProduct p : products) {


[Link]("<tr>");
[Link]("<td>" + [Link]() + "</td>");
[Link]("<td>" + [Link]() + "</td>");
[Link]("<td>" + [Link]() + "</td>");
[Link]("</tr>");
}
[Link]("</table>");
[Link]("</body></html>");
[Link]();
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
[Link]
package [Link];

import [Link];
import [Link];

import [Link];

public class HibernateUtil {

static SessionFactory sessionFactory;

public static SessionFactory buildSessionFactory() {

if (sessionFactory == null) {
Configuration cfg = new Configuration();
[Link]("[Link]");
[Link]([Link]);

sessionFactory = [Link]();
}
return sessionFactory;
}
}

[Link]
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"[Link]

<hibernate-configuration>

<session-factory>

<property
name="[Link].driver_class">[Link]</property>
<property
name="[Link]">jdbc:mysql://localhost:3306/ecommerce</property>
<property name="[Link]">root</property>
<property name="[Link]">Pooja@1998</property>

<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="[Link]">update</property>

</session-factory>
</hibernate-configuration>

[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Product</title>
</head>
<body>

<h1>Add Product</h1>

<form action="add-product" method="post">


<fieldset>
<legend>Add Product Form</legend>

Product Name: <input type="text" name="name" id="name" /> <br>


<br> Product Price: <input type="text" name="price" id="price" /><br>
<br> <input type="submit" value="Add Product">
</fieldset>
</form>

</body>
</html>

[Link]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>Welcome to hibernate CRUD application</h1>


<nav>
<a href="add-product">Add Product</a>||
<a href="read-product"> Read Product</a>
</nav>

</body>
</html>

[Link]
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>MPhasis_Feb_Hibernate</groupId>
<artifactId>MPhasis_Feb_Hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>[Link]-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<!-- [Link] -->


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>

<!-- [Link] -->


<dependency>
<groupId>[Link]</groupId>
<artifactId>hibernate-core</artifactId>
<version>[Link]</version>
</dependency>

</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

You might also like