For inserting record into database we have to follow the following steps

Step 1: Create table from your database(i am using MYSQL )

for doing any operations in hibernate we have to  create table with primary key

create table employee(id int(10)  primary key(id),name varchar(30),address varchar(30));

now table is created as below



















Step 2: create form with id,name,address fields

<form action="s">
    Employee Id:<input type="text" name="id"><br>
    Employee Name:<input type="text" name="name"><br>
    Employee Address:<input type="text" name="address"><br>
    <input type="submit" value="submit">
</form>

Step 3: create POJO class with table fields

package a;

public class Employee {

    int id;
    String name;
    String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
 
 
}


Step 4: generate hibernate configuration file (hibernate.cfg.xml)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/xxx</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="a/Employee.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


Step 5: create DAO class with inserting code as below

package a;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class EmployeeServlet extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
        String s=request.getParameter("id");
        int i=Integer.parseInt(s);
        String s1=request.getParameter("name");
        String s2=request.getParameter("address");
 Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session hsession=sf.openSession();
Transaction tx=hsession.beginTransaction();
Employee e=new Employee();

e.setId(i);
e.setName(s1);
e.setAddress(s2);

hsession.save(e);

tx.commit();


RequestDispatcher rd=request.getRequestDispatcher("success.jsp");
rd.include(request, response);


hsession.close();
sf.close();
}
        
        
    }

Step 6: provide hibernate reverse engineering wissard

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


Step 7: generate hibernate mapping file from database table 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 8, 2013 10:36:24 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="a.Employee" table="employee" catalog="xxx">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" />
        </property>
        <property name="address" type="string">
            <column name="address" length="30" />
        </property>
    </class>
</hibernate-mapping>


Step 8: now run your application (here i am using NetBeans ID)

check table for output weather your entered details are entered into database or not








0 comments:

Post a Comment