Struts2.0 insert data in database table

Struts2.0 insert data in database table

Previous Home Next

 

In struts 2.0 framework insert 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 Insert Data Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<<s:actionerror/>
<s:form action="insert">
<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:submit value="Submit"></s: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="insert" class="org.r4r.UserListInsertAction">
<result name="success">/index.jsp</result>
<result name="input">/index.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
</struts>

UserListInsertAction.java

package org.r4r;

import com.opensymphony.xwork2.ActionSupport;

public class UserListInsertAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	String name;
	String address;
	String city;
	String state;
	DAO dao=new DAO();
	
	public String execute(){
	if(dao.insert(name, address, city, state)){
	this.addActionError("You are successfully insert data.");
	return "success";}
	else
	this.addActionError("Data is not successfully insert");
	return "error";
	}
	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;

public class DAO {
	public boolean insert(String name,String address,String city,String state){
	try{
	Connection con=ConnectionProvider.getConnection();
	PreparedStatement stmt=con.prepareStatement("insert into userlist values(?,?,?,?)");
	stmt.setString(1,name);
	stmt.setString(2,address);
	stmt.setString(3,city);
	stmt.setString(4,state);
	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;
	}
}

Output




Previous Home Next