Hibernate

Servlet And Hibernate Connectivity:-
Previous Home Next

This example shows the connectivity between servlet and hibernate

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">

<!-- r4r.co.in. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="personal.hbm.xml"/>
</session-factory>
</hibernate-configuration>

personal.java(pojo class)

package mypack;

public class personal {
int id;
String name;
String Student;
String specializationin;

public personal() {
super();

}
public personal(String name, String student, String specializationin) {
super();
this.name = name;
Student = student;
this.specializationin = specializationin;
}
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;
}
public String getStudent() {
return Student;
}
public void setStudent(String student) {
Student = student;
}
public String getSpecializationin() {
return specializationin;
}
public void setSpecializationin(String specializationin) {
this.specializationin = specializationin;
}
}

personal.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping>
<class name="mypack.personal">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="student"/>
<property name="specializationin"/>
</class>
</hibernate-mapping>

index.jsp

<html>
<body>
<form action="Detail" method="get">
Name<input type="text" name="id" value=""/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Detail.java(servlet class)

package mypack;

import java.io.IOException;
import java.io.PrintWriter;
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.cfg.Configuration;

public class Detail extends HttpServlet {
private static final long serialVersionUID = 1L;
SessionFactory f;
Session session;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("id");
int name1=Integer.parseInt(name);
response.setContentType("text/html");
PrintWriter out=response.getWriter();

Configuration cfg=new Configuration().configure();
f=cfg.buildSessionFactory();
session=f.openSession();
personal p=(personal) session.get(personal.class,name1);

out.println("<html");
out.println("<body><table border=1>");
out.println("<tr><td>name</td><td>student</td><td>specialisationin</td></tr>");
out.println("following record are fetched:");
out.print("<tr><td>");
out.print(p.getName());
out.print("</td>");
out.print("<td>");
out.print(p.getStudent());
out.print("</td");
out.print("<td>");
out.print(p.getSpecializationin());
out.print("</td");
out.print("</tr>");
out.println("</table></body>");
out.println("</html>");
session.close();
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Detail</servlet-name>
<servlet-class>mypack.Detail</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Detail</servlet-name>
<url-pattern>/Detail</url-pattern>
</servlet-mapping>
</web-app>
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
Name:
eMail:
Comment / Feedback: