Struts2.0 name wise update data in database table

Struts2.0 name wise update data in database table

Previous Home Next

 

In struts 2.0 framework name wise update data in database table through the JDBC API.

Require tools to run this application
1. Struts2.0 jar file
2. Tomcat server
3. Database Oracle10g
 Directory Structure of Update Data Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<s:form action="fetch">
<s:textfield name="name" label="Name"/>
<s:submit value="Submit"/>
</s:form>

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>
<package name="demo" extends="struts-default">
<action name="fetch" class="org.r4r.UserListAction">
<result name="success">/fetch.jsp</result>
</action>
<action name="update" class="org.r4r.UserListAction" method="update">
<result name="success">/index.jsp</result>
<result name="input">/fetch.jsp</result>
<result name="error">/fetch.jsp</result>
</action>
</package>
</struts> 

UserListAction.java

package org.r4r;

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

public class UserListAction extends ActionSupport{	
	private static final long serialVersionUID = 1L;
	String name;
	String address;
	String city;
	String state;
	private UserList userlist;
	private List<UserList> userlistlist;
	DAO dao=new DAO();
	public String execute(){
	userlistlist=dao.fetch(getName());
	return "success";
	}
	public String update(){
	if(dao.update(getName(),getAddress(), getCity(), getState())){
	//this.addActionError("You are successfully update");
	return "success";}
	else
	this.addActionError("Your data is not Update");
	return "error";
	}
	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;
	}
}

UserList.java

package org.r4r;

public class UserList {
	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 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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

public class DAO {
	public List<UserList> fetch(String name){
	try{
	Connection con=ConnectionProvider.getConnection();
	PreparedStatement stmt=con.prepareStatement("select * from userlist where name=?");
	stmt.setString(1, name);
	ResultSet rset=stmt.executeQuery();
	UserList userlist;
	List<UserList> list=new ArrayList<UserList>();
	while(rset.next()){
	userlist=new UserList();
	userlist.setName(rset.getString(1));
	userlist.setAddress(rset.getString(2));
	userlist.setCity(rset.getString(3));
	userlist.setState(rset.getString(4));
	list.add(userlist);
	}
	return list;		
	}catch(Exception e){
	System.out.println(e);
	}
	return null;
	}
	public boolean update(String name,String address,String city,String state){
	try{
	Connection con=ConnectionProvider.getConnection();
	PreparedStatement stmt=con.prepareStatement("update userlist set name=?,address=?,city=?,state=? where name=?");
	stmt.setString(1,name);
	stmt.setString(2,address);
	stmt.setString(3,city);
	stmt.setString(4,state);
	stmt.setString(5,name);
	stmt.executeQuery();
	return true;
	}catch(Exception e){
	System.out.println(e);
	}
	return false;
	}
	
}

ConnectionProvider.java

package org.r4r;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionProvider {
	public static Connection getConnection(){
	Connection con=null;
	try{
	Class.forName("oracle.jdbc.driver.OracleDriver");
	con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
	}catch(Exception e){
	System.out.println(e);
	}
	return con;
	}
}


fetch.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<s:actionerror/>
<s:form action="update">
<s:iterator value="userlistlist" var="userlist">
<s:textfield name="name" label="Name"/>
<s:textfield name="address" label="Address"/>
<s:textfield name="city" label="City"/>
<s:textfield name="state" label="State"/>
</s:iterator>
<s:submit value="Submit"/>

</s:form>

Output




Previous Home Next