Insert,fetch,update&delete operation through struts2 & hibernate3.0 framework

Insert,fetch,update&delete operation through struts2 & hibernate3.0 framework

Previous Home Next

 

This application provide the concept of insert, fetch, update, name wise fetch and delete row wise in a single application using struts 2.0 and Hibernate 3.0 framework.

Require tools to run this application
1. Struts2.0 jar file
2. Tomcat server
3. Database Oracle10g
4. Hibernate 3.0 jar file

Directory Structure of All Query in Struts 2.0 and Hibernate3.0 Using MyEclipse IDE



index.jsp

<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing="10" bordercolor="red" border="5">
<tr><td align="center" height="30" width="250">
<a href="insert.jsp" style="color: gray;text-decoration: none">Data Insert</a>
</td></tr>
<tr><td align="center" height="30" width="250">
<a href="fetch.jsp" style="color: gray;text-decoration: none">Data Fetch</a>
</td></tr>
<tr><td align="center" height="30" width="250">
<a href="nameUpdate.jsp" style="color: gray;text-decoration: none">Update Data</a>
</td></tr>
<tr><td align="center" height="30" width="250">
<a href="deletedata.jsp" style="color: gray;text-decoration: none">Delete Row Wise Data</a>
</td></tr>
</table></center>
</body>
</html>

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>
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping></web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--Insert data in database table-->
<package name="demo" extends="struts-default">
<action name="insert" class="org.r4r.InsertAction">
<result name="success">/insert.jsp</result>
<result name="input">/insert.jsp</result>
</action>
<!--Fetch all data-->
<action name="alldatafetch" class="org.r4r.FetchAction">
<result name="success">/alldatafetch.jsp</result>
</action>
<!--Fetch name wise data-->
<action name="nameFetch" class="org.r4r.FetchAction" method="nameFetch">
<result name="success">/alldatafetch.jsp</result>
</action>
<!--Name wise update data-->
<action name="fetch" class="org.r4r.FetchAction" method="nameFetch">
<result name="success">/updateFetch.jsp</result>
</action>
<action name="update" class="org.r4r.FetchAction" method="update">
<result name="success">/index.jsp</result>
</action>
<!--Row wise data delete-->
<action name="rowdatadelete" class="org.r4r.FetchAction">
<result name="success">/deletedatafetch.jsp</result>
</action>
<action name="delete" class="org.r4r.FetchAction" method="delete">
<result name="success" type="chain">rowdatadelete</result>
</action>
<!--Name wise row data delete-->
<action name="nameDelete" class="org.r4r.FetchAction" method="nameFetch">
<result name="success">/nameDeleteFetch.jsp</result>
</action>
<action name="delete1" class="org.r4r.FetchAction" method="delete">
<result name="success">/nameDelete.jsp</result>
</action>
</package>
</struts>  

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

<!-- Generated by MyEclipse Hibernate Tools.-->
<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="alloperation.hbm.xml"/>
    </session-factory>

</hibernate-configuration> 

alloperation.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="org.r4r.UserList" table="userlist1">
    <id name="id" type="int">
    <generator class="increment"/>
    </id>
    <property name="name"/>
    <property name="address"/>
    <property name="city"/>
    <property name="state"/>
    </class>

</hibernate-mapping>

InsertAction.java

package org.r4r;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;

public class InsertAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	UserList userlist;
	List<UserList> userlistlist;
	DAO dao=new DAO();
	
	public void validate(){
		if(userlist.name.length()==0)
			this.addFieldError("userlist.name","Name is required");
		if(userlist.address.length()==0)
			this.addFieldError("userlist.address","Address is required");
		if(userlist.city.length()==0)
			this.addFieldError("userlist.city","City is required");
		if(userlist.state.length()==0)
			this.addFieldError("userlist.state","State is required");
	}
	public String execute(){
		dao.insert(userlist);
		this.addActionError("You are successfully insert data");
		return "success";
		
	}
	public String fetch(){
		userlistlist=dao.list();
		return "success";
	}

	public UserList getUserlist() {
		return userlist;
	}

	public void setUserlist(UserList userlist) {
		this.userlist = userlist;
	}
	public List<UserList> getUserlistlist() {
		return userlistlist;
	}
	public void setUserlistlist(List<UserList> userlistlist) {
		this.userlistlist = userlistlist;
	}
}

UserList.java

package org.r4r;

public class UserList {
	int id;
	String name;
	String address;
	String city;
	String state;	
	public UserList() {
		super();
		// TODO Auto-generated constructor stub
	}
	public UserList(String name, String address, String city, String state) {
		super();
		this.name = name;
		this.address = address;
		this.city = city;
		this.state = state;
	}
	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 getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
}

 
DAO.java

package org.r4r;

import java.util.List;
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 DAO {
	public void insert(Object o){
		Configuration cfg=new Configuration().configure();
		SessionFactory f=cfg.buildSessionFactory();
		Session session=f.openSession();
		Transaction t=session.beginTransaction();
		session.save(o);
		t.commit();
		session.close();		
	}
	@SuppressWarnings("unchecked")
	public List<UserList> list(){
		Configuration cfg=new Configuration().configure();
		SessionFactory f=cfg.buildSessionFactory();
		Session session=f.openSession();
		Transaction t=session.beginTransaction();
		List<UserList> userlist=null;
		try{
			Query q=session.createQuery("from UserList");
			userlist=q.list();
			
		}catch(HibernateException he){
			System.out.println(he);
		}
		t.commit();
		session.close();
		return userlist;
	}
	@SuppressWarnings("unchecked")
	public List<UserList> nameFetch(String name){
		Configuration cfg=new Configuration().configure();
		SessionFactory f=cfg.buildSessionFactory();
		Session session=f.openSession();
		Transaction t=session.beginTransaction();
		List<UserList> userlist=null;
		try{
			Query q=session.createQuery("select userlist from UserList userlist where userlist.name='"+name+"'");
			userlist=q.list();
			
		}catch(HibernateException he){
			System.out.println(he);
		}
		t.commit();
		session.close();
		return userlist;
	}
	public UserList update(int id,String name,String address,String city,String state){
		Configuration cfg=new Configuration().configure();
		SessionFactory f=cfg.buildSessionFactory();
		Session session=f.openSession();
		Transaction t=session.beginTransaction();
		UserList userlist=(UserList) session.load(UserList.class, id);
		userlist.setName(name);
		userlist.setAddress(address);
		userlist.setCity(city);
		userlist.setState(state);
		if(null!=userlist){
			session.update(userlist);
		}
		t.commit();
		session.close();
		return userlist;
	}
	public UserList delete(int id){
		Configuration cfg=new Configuration().configure();
		SessionFactory f=cfg.buildSessionFactory();
		Session session=f.openSession();
		Transaction t=session.beginTransaction();
		UserList userlist=(UserList) session.load(UserList.class, id);
		if(null!=userlist){
			session.delete(userlist);
		}
		t.commit();
		session.close();
		return userlist;
	}
}

FetchAction.java

package org.r4r;

import java.util.List;

public class FetchAction {
	int id;
	String name;
	String address;
	String city;
	String state;
	UserList userlist;
	List<UserList> userlistlist;
	DAO dao=new DAO();
	public String execute(){
		userlistlist=dao.list();
		return "success";
	}
	public String nameFetch(){
		userlistlist=dao.nameFetch(getName());
		return "success";
	}
	public String update(){
		dao.update(getId(), getName(),getAddress(),getCity(),getState());
		return "success";
	}
	public String delete(){
		dao.delete(getId());
		return "success";
	}
	public UserList getUserlist() {
		return userlist;
	}
	public void setUserlist(UserList userlist) {
		this.userlist = userlist;
	}
	public List<UserList> getUserlistlist() {
		return userlistlist;
	}
	public void setUserlistlist(List<UserList> userlistlist) {
		this.userlistlist = userlistlist;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}

insert.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:actionerror/>
<s:form action="insert">
<s:textfield name="userlist.name" label="Name"/>
<s:textfield name="userlist.address" label="Address"/>
<s:textfield name="userlist.city" label="City"/>
<s:textfield name="userlist.state" label="State"/>
<s:submit value="Insert Data"/>
</s:form>
<br/><br/><br/>
<a href="index.jsp">Back</a>

alldatafetch.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing="2" bordercolor="red" border="5">
<tr>
<th align="center" height="30" width="250">Name</th>
<th align="center" height="30" width="250">Address</th>
<th align="center" height="30" width="250">City</th>
<th align="center" height="30" width="250">State</th>
</tr>
<s:iterator value="userlistlist" var="userlist">
<tr>
<td align="center" height="30" width="250"><s:property value="name"/></td>
<td align="center" height="30" width="250"><s:property value="address"/></td>
<td align="center" height="30" width="250"><s:property value="city"/></td>
<td align="center" height="30" width="250"><s:property value="state"/></td>
</tr>
</s:iterator>
</table>
<br/><br/>
<a href="fetch.jsp" style="font-size: 18px;">Back</a>
</center>
</body>
</html> 

deletedata.jsp

<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing="10" bordercolor="red" border="5">
<tr><td align="center" height="30" width="250">
<a href="rowdatadelete" style="color: gray;text-decoration: none">All data Fetch to delete</a></td></tr>
<tr><td align="center" height="30" width="250">
<a href="nameDelete.jsp" style="color: gray;text-decoration: none">Name wise data fetch to delete</a></td></tr>
</table>
<br/><br/>
<a href="index.jsp" style="font-size: 18px;">Back</a>
</center>
</body>
</html>

deletedata.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing="2" bordercolor="red" border="5">
<tr>
<th align="center" height="30" width="250">Name</th>
<th align="center" height="30" width="250">Address</th>
<th align="center" height="30" width="250">City</th>
<th align="center" height="30" width="250">State</th>
<th align="center" height="30" width="250">Delete</th>
</tr>
<s:iterator value="userlistlist" var="userlist">
<tr>
<td align="center" height="30" width="250"><s:property value="name"/></td>
<td align="center" height="30" width="250"><s:property value="address"/></td>
<td align="center" height="30" width="250"><s:property value="city"/></td>
<td align="center" height="30" width="250"><s:property value="state"/></td>
<td align="center" height="30" width="250"><a href="delete?id=<s:property value='id'/>">Delete</a></td>
</tr>
</s:iterator>
</table>
<br/><br/>
<a href="deletedata.jsp" style="font-size: 18px;">Back</a>
</center>
</body>
</html>

deletedatafetch.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing="2" bordercolor="red" border="5">
<tr>
<th align="center" height="30" width="250">Name</th>
<th align="center" height="30" width="250">Address</th>
<th align="center" height="30" width="250">City</th>
<th align="center" height="30" width="250">State</th>
<th align="center" height="30" width="250">Delete</th>
</tr>
<s:iterator value="userlistlist" var="userlist">
<tr>
<td align="center" height="30" width="250"><s:property value="name"/></td>
<td align="center" height="30" width="250"><s:property value="address"/></td>
<td align="center" height="30" width="250"><s:property value="city"/></td>
<td align="center" height="30" width="250"><s:property value="state"/></td>
<td align="center" height="30" width="250"><a href="delete?id=<s:property value='id'/>">Delete</a></td>
</tr>
</s:iterator>
</table>
<br/><br/>
<a href="deletedata.jsp" style="font-size: 18px;">Back</a>
</center>
</body>
</html>

fetch.jsp

<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing="10" bordercolor="red" border="5">
<tr><td align="center" height="30" width="250">
<a href="alldatafetch" style="color: gray;text-decoration: none">All Data Fetch</a></td></tr>
<tr><td align="center" height="30" width="250">
<a href="nameFetch.jsp" style="color: gray;text-decoration: none">Name Wise Fetch</a></td></tr>
</table>
<br/><br/>
<a href="index.jsp" style="font-size: 18px;">Back</a>
</center>
</body>
</html>

nameDelete.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:actionerror/>
<s:form action="nameDelete">
<s:textfield name="name" label="Name"/>
<s:submit value="Submit"/>
</s:form>
<br/><br/><br/>
<a href="index.jsp">Back</a>

nameDeleteFetch.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head></head>
<body><center>
<table cellpadding="0" cellspacing

Output




Previous Home Next