11:04 pm - Saturday May 27, 2017

Hibernate servlet jsp inserting Data Using Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Hibernate integrate with Servlet:

To integrate hibernate and servlet, Inputs are get through servlet class and pass values through argument to hibernate business logic class.

<strong>SaveStudentControl ctrl = new SaveStudentControl();</strong>

<strong>ctrl.addNewStudent(id,regNo,name,mark1,mark2,degree,mobNo,mailId);</strong>

All other process is same as hibernate.

Project Structure:

 

 

 

 

 

 

 

 

 

 

 

JAR Files:

 

 

 

 

 

 

 

 

 

 

 

Home.java:

package com.candidjava;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Home extends HttpServlet {

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id;
		String regNo;
		String name;
		String mark1;
		String mark2;
		String degree;
		String mobNo;
		String mailId;

		id = request.getParameter("id");
		regNo = request.getParameter("regno");
		name = request.getParameter("name");
		mark1 = request.getParameter("mark1");
		mark2 = request.getParameter("mark2");
		degree = request.getParameter("degree");
		mobNo = request.getParameter("mobNo");
		mailId = request.getParameter("mail");
		HttpSession session = request.getSession(true);
		try {
			SaveStudentControl ctrl = new SaveStudentControl();
			ctrl.addNewStudent(id, regNo, name, mark1, mark2, degree, mobNo,
					mailId);
			response.sendRedirect("Success");
		} catch (Exception e) {

			e.printStackTrace();
		}

	}
}

SaveStudentControl.java:

package com.candidjava;

import java.sql.*;
import java.util.List;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class SaveStudentControl {

	private static SessionFactory sessionFactory;

	private Session getSession() {
		Session s = null;
		try {
			sessionFactory = new Configuration().configure(
					"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
			s = sessionFactory.openSession();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return s;
	}

	public void addNewStudent(String stuid, String sturegno, String stuname,
			String stumark1, String stumark2, String degree, String mobileno,
			String mailid) throws Exception {
		try {
			Session s = getSession();
			Transaction tx = s.beginTransaction();
			Student stu = new Student();

			stu.setStuid(stuid);
			stu.setSturegno(sturegno);
			stu.setStuname(stuname);
			stu.setStumark1(stumark1);
			stu.setStumark2(stumark2);
			stu.setDegree(degree);
			stu.setMobileno(mobileno);
			stu.setMailid(mailid);
			s.save(stu);

			tx.commit();
			System.out.println("\n\n Details Added \n");
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			System.out.println("error");
		}

	}

}// end of class

Student.java:

package com.candidjava;
import java.io.*;
public class Student implements Serializable
{
	private long id;
	private String stuid;
	private String sturegno;
	private String stuname;
	private String stumark1;
	private String stumark2;
	private String degree;
	private String mobileno;
	private String mailid;

	public long getId() {
		return id;
	}

	public String getStuid() {
		return stuid;
	}
	public String getSturegno() {
		return sturegno;
	}
	public String getStuname() {
		return stuname;
	}
	public String getStumark1() {
		return stumark1;
	}
	public String getStumark2() {
		return stumark2;
	}

	public String getDegree() {
		return degree;
	}

	public String getMobileno() {
		return mobileno;
	}
	public String getMailid() {
		return mailid;
	}
	public void setId(long string) {
		id = string;
	}

	public void setStuid(String string) {
		stuid = string;
	}
	public void setSturegno(String string) {
		sturegno = string;
	}
	public void setStuname(String string) {
		stuname = string;
	}
	public void setStumark1(String string) {
		stumark1 = string;
	}
	public void setStumark2(String string) {
		stumark2 = string;
	}
	public void setDegree(String string) {
		degree = string;
	}

	public void setMobileno(String string) {
		mobileno = string;
	}
	public void setMailid(String string) {
		mailid = string;
	}

}

Success.java:

package com.candidjava;

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;

/**
 * Servlet implementation class Success
 */
public class Success extends HttpServlet {

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();

		writer.println("<html>" + "<body bgcolor='pink'>" + "<center>"
				+ "Details Added Successfully" + "</center>" + "</body>"
				+ "</html>");
	}

}

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="studentFactory">
		<property name="connection.driver_class">
			oracle.jdbc.OracleDriver
	        </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.pool_size">5</property>
		<!-- SQL dialect -->
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
	        </property>
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">false</property>
		<property name="hbm2ddl.auto">create</property>
		<mapping resource="com\\xml\\Student.hbm.xml" />
	</session-factory>
</hibernate-configuration>

Student.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.candidjava.Student" table="Student">
		<id name="id" type="long" column="Id">
			<generator class="increment" />
		</id>
		<property name="stuid" column="StuId" not-null="true" />
		<property name="sturegno" column="StuRegNO" />
		<property name="stuname" column="StuName" />
		<property name="stumark1" column="StuMark1" />
		<property name="stumark2" column="StuMark2" />
		<property name="degree" column="Degree" />

		<property name="mobileno" column="MobileNo" />
		<property name="mailid" column="MailId" />
	</class>
</hibernate-mapping>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>ServletandHibernate</display-name>
	<servlet>
		<description>
		</description>
		<display-name>Home</display-name>
		<servlet-name>Home</servlet-name>
		<servlet-class>com.candidjava.Home</servlet-class>
	</servlet>
	<servlet>
		<description>
		</description>
		<display-name>Success</display-name>
		<servlet-name>Success</servlet-name>
		<servlet-class>com.candidjava.Success</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Home</servlet-name>
		<url-pattern>/Home</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>Success</servlet-name>
		<url-pattern>/Success</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

OUTPUT:

DOWNLOAD+SOURCECODE

BACK

Filed in: Z-A Hibernate codes

Comments are closed.