4:01 pm - Saturday February 25, 2017

Hibernate Struts 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

 

  • This application will use  join struts and hibernate applications.
  • Here First  we will  get input from front view html or Jsp page.
  • Then it will pass model and cotroller using struts application.
  • Then In controller page input will save the database using Hibernate application.
  • After save the inputs will pass the front view.

 Project Structure:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JarFiles:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AddStudentControl.java:


  • Here we will use sessionFactory for create connetion Oracle database.
    • sessionFactory = new Configuration().configure

(“com\\xml\\hibernate.cfg.xml”).buildSessionFactory();

  • Then we will get input from AddController.java through arguments and then save the database using session.
    • stu.setStuname(stuname);
    • s.save(stu);
  • Then we will use  Transaction statement for  begin and commit the database.
    • Transaction tx= s.beginTransaction();
    • tx.commit();
package com.candidjava;

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

public class AddStudentControl {

	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 args, String args1, String args2)
			throws Exception {
		try {
			Session s = getSession();
			Transaction tx = s.beginTransaction();
			Student stu = new Student();

			System.out.println("Student name");
			String stuname = args;
			String studegree = args1;
			String stumobileno = args2;

			stu.setStuname(stuname);
			stu.setStudegree(studegree);
			stu.setStumobileno(stumobileno);

			s.save(stu);

			tx.commit();

			System.out.println("\n\n Details Added \n");
		} catch (HibernateException e) {
			System.out.println("error1" + e.getMessage());
			System.out.println("error1");
		}

	}

}// end of class

MyController.java: 

  • It is a Controller class .It will extends Action class.
  • MyModel m = (MyModel)fm;//Here model class object will store in the variable name m.
  • String s=m.getName();String s1=m.getDegree();String s2=m.getMobileno();//Here get input from model class.
  • AddStudentControl ctrl = new AddStudentControl();//Here we will create object for AddStudentControl class
  • ctrl.addNewStudent(s,s1,s2);//Here pass the input to AddNewStudent method through Arguments from MyController class.
package com.candidjava;

import javax.servlet.http.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MyController extends Action {
	public ActionForward execute(ActionMapping map, ActionForm fm,
			HttpServletRequest req, HttpServletResponse res) throws Exception {
		HttpSession ses = req.getSession(true);

		MyModel m = (MyModel) fm;
		String s = m.getName();
		String s1 = m.getDegree();
		String s2 = m.getMobileno();

		System.out.println(s);
		System.out.println(s1);
		System.out.println(s2);

		AddStudentControl ctrl = new AddStudentControl();
		ctrl.addNewStudent(s, s1, s2);

		ses.setAttribute("s", s);
		ses.setAttribute("s1", s1);
		ses.setAttribute("s2", s2);
		if (s.equals("")) {
			return map.findForward("error");
		}
		return map.findForward("success");

	}
}

MyModel.java:

  • It is a Model class. It will extends ActionForm class which is  a Serializable class. Then here we will use get and set method.
    • private String name;
    • public String getName(){return name;}
    • public void setName(String n){name=n;      }
package com.candidjava;

import org.apache.struts.action.ActionForm;

public class MyModel extends ActionForm {
	private String name;
	private String degree;
	private String mobileno;

	public void setName(String n) {
		name = n;
	}

	public String getName() {
		return name;
	}

	public void setDegree(String n) {
		degree = n;
	}

	public String getDegree() {
		return degree;
	}

	public void setMobileno(String n) {
		mobileno = n;
	}

	public String getMobileno() {
		return mobileno;
	}

}

Student.java:

  • It is a Pojo class. This class Must be a Serializable. Then here we will use get and set method.
    • private long id;
    • public long getId() {return id;}
    • public void setId(long String) {id = String;}
package com.candidjava;

import java.io.*;

public class Student implements Serializable {
	private long id;
	private String stuname;
	private String studegree;
	private String stumobileno;

	public long getId() {
		return id;
	}

	public String getStuname() {
		return stuname;
	}

	public String getStudegree() {
		return studegree;
	}

	public String getStumobileno() {
		return stumobileno;
	}

	public void setId(long string) {
		id = string;
	}

	public void setStuname(String string) {
		stuname = string;
	}

	public void setStudegree(String string) {
		studegree = string;
	}

	public void setStumobileno(String string) {
		stumobileno = string;
	}

}

web.xml:

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>//This tag will load the ActionServlet class.

  • <init-param><param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value> </init-param>//This tag will use configure the struts-config.xml.

</servlet>

  • <servlet-mapping><servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern> </servlet-mapping>//here *.do invoke the ActionServlet class.

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>

		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>Login.jsp</welcome-file>
	</welcome-file-list>

	<taglib>
		<taglib-uri>/struts-html</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>

	<security-constraint>
		<web-resource-collection>
			<web-resource-name>My secure resources</web-resource-name>
			<description>Resources to be placed under security control.</description>
			<url-pattern>/private/*</url-pattern>
			<url-pattern>/registered/*</url-pattern>
		</web-resource-collection>
		<auth-constraint>
			<role-name>guest</role-name>
		</auth-constraint>
	</security-constraint>

	<login-config>
		<auth-method>BASIC</auth-method>
		<realm-name>WebApp</realm-name>
	</login-config>

	<security-role>
		<description>The role allowed to access our content</description>
		<role-name>guest</role-name>
	</security-role>

</web-app>

struts-config.xml:

<form-beans><form-bean type=”com.candidjava.MyModel” name =”model” />

</form-beans>//Beans tag will use for model class.Here name=”model” which is same as the controller name=”model”.

  • <action-mappings><action path=”/myActionForm” type=”com.candidjava.MyController” name=”model” input=”/Login.jsp”>//Action mapping tag will use both view and controller.Action tag will use for controller class.Here path=”/myActionForm” which is same as the front view Login.jsp action=”/myActionForm”.
  • <forward name=”success” path=”/Success.jsp” />//Forward tag will use for view page.It will forward  input to the view page
  • <forward name=”error” path=”/Fail.jsp” /></action></action-mappings>
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
	<form-beans>
		<form-bean type="com.candidjava.MyModel" name="model" />
	</form-beans>
	<action-mappings>
		<action path="/myActionForm" type="com.candidjava.MyController"
			name="model" input="/Login.jsp">
			<forward name="success" path="/Success.jsp" />
			<forward name="error" path="/Fail.jsp" />

		</action>
	</action-mappings>
</struts-config>

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">update</property>
		<mapping resource="com\\xml\\Student.hbm.xml" />
	</session-factory>
</hibernate-configuration>

Student.hbm.xml:

  • <class name=”com.candidjava.Student” table=”student” >//This tag will use for mapping Pojo class name and  table name.
  • <id name=”id” type=”long” column =”ID”><generator class=”increment”/></id>//This tag will use for generating primary key id and also increment the id value.
  • <property name=”name” column=”name” not-null=”true”/>//This property will use for mapping the pojo class veriable name(name=”name” ) and  table attribute name(column=”name”).
  • </class>”/>//End of the class.
<?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="StudentDet">
		<id name="id" type="long" column="Id">
			<generator class="increment" />
		</id>
		<property name="stuname" column="StuName" not-null="true" />
		<property name="studegree" column="Degree" />
		<property name="stumobileno" column="MobileNo" />

	</class>
</hibernate-mapping>

OUTPUT:

 



  

DOWNLOAD+SOURCECODE


BACK

Filed in: Z-A Hibernate codes

Comments are closed.