Insert data into database with Servlet and JSP Using Hibernate
Previous Home Next

Here we will discuss How to Send Data in Data Base (INSERT Operation in One Table) Using Servlet and JSP in Hibernate. Hibernate provide the facility without writing procedure for the database connectivity for sending the data into the database. For Send Data in Data Base (INSERT Operation in One Table) Using Servlet and JSP in Hibernate we need to follow some basic step which are given below:

Step 1. DataBase Creation (MySQL)

(a). create Database hibernate_pro; (Store all table into this database)

(b). use hibernate_pro; (here use means is activate means all database activity store in this database like as table)

(c). create table InsertData
id int auto_increment not null primary key,
first_name varchar(50),
last_name varchar(50),
age varchar(50)

(d). select * from InsertData;

Step 2. Prepration of application on NetBeans

1. Take a new Project name is \'HibernateApplicationUsingServlet\' First of all we will select to File Menu Wizard -> then click on NewProject -> Click on JavaWeb -> then Web Application -> click on Next -> Write the name of the Project -> select the server on which want to deploy the project -> click on Hibernate -> click on database connection -> hibernate_pro -> finish. Then given outlook will be the project on NetBeans


org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/hibernate_pro root root

Step 3. Take a JSP page for the user view point.

For this we need to go in File Menu Wizard -> click on New File Wizard -> click on web and then click on also jsp -> next -> jsp page name give here which you want -> click on finish.


<%@page contentType=\"text/html\" pageEncoding=\"UTF-8\"%>
<!DOCTYPE html>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
        <form action=\"MyServlet\" method=\"post\">
        First Name      <input type=\"text\" name=\"fist_name\"/>
        Last Name       <input type=\"text\" name=\"last_name\"/>
        Age             <select name=\"age\">
                        <option>--Select Age--</option>
                        <option>0 to 10</option>
                        <option>10 to 20</option>
                        <option>20 to 30</option>
                        <option>30 to above</option>
        <input type=\"submit\" value=\"Submit\"/>


Step 4. Create a reveng file for the connectivity for java class to database and for creating the pojo class.

for this go in file menu wizard -> new file -> search hibernate -> then click on opposite side given hibernate reverse engineering file-> next -> click on table which we create in the database -> add > finish .

hibernate-revenge file

 <?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC
\"-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN\" \"http://hibernate.sourceforge.net/
  <schema-selection match-catalog=\"hibernate_pro\"/>
  <table-filter match-name=\"InsertData\"/>

Step 5. Now we need to create POJO class for sending the data into the data base.

Remember -> Before creating the POJO class we need to create the package to store the POJO class into the package.Go in the File Menu wizard -> search hibernate -> then click on opposite side given hibernate POJO class file-> next -> both file should be in for the pojo class (hibernate.cfg & hibernate.revenge file ) -> slect the package > finish .

Step 6: Now we need to create a action page of servlet. for creating the servlet page we need to go in File Menu wizard-> new file-> web & opposite side search to servlet click on it -> mark on add information as given in the image below and then click on finish.

Step 7. Add this code into the default servlet page code

 public class MyServlet extends HttpServlet 

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String fist_name, last_name, age;
        fist_name = request.getParameter(\"fist_name\");
        last_name = request.getParameter(\"last_name\");
        age = request.getParameter(\"age\");
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
            Session s = sf.openSession();
            Transaction tr = s.beginTransaction();
        InsertData id = new InsertData(fist_name, last_name, age, new Date());
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println(\"<!DOCTYPE html>\");
            out.println(\"<title>Servlet MyServlet</title>\");            
            out.println(\"<h1>Welcome \" + fist_name + \"</h1>\");

Point to remember - servlet file should be in the web.xml file as given below:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<web-app version=\"3.1\" xmlns=\"http://
xmlns.jcp.org/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 

Step 8. After all doing the Now we need to build and run the project for this we need to go in the menu wizard -> click on run menu -> click on clean and build the project -> then click on the run when successfully build the project.

input the given form as click on submit welcome message will come on the screen.

check the data base for the data is going in the data base or not.

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Comment / Feedback: