11:04 pm - Saturday December 20, 2014

Struts with jdbc Integration Tutorial with example program

Project Structure:

 

 

 

 

 

 

 

 

 

 

Jar Files:

 

 

 

 

 

 

 

 Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

 

 InsertDataBase.java:

import java.sql.*;

public class InsertDataBase {
	public void insertData1(String name, String degree, String mobno)
			throws Exception {
		System.out.println("jdbc connection");
		Class.forName("oracle.jdbc.driver.OracleDriver");
		String url = "jdbc:oracle:thin:@localhost:1521:XE";
		String un = "system";
		String pwd = "system";
		String name1 = name;
		String degree1 = degree;
		String mobno1 = mobno;
		try {
			Connection c = DriverManager.getConnection(url, un, pwd);
			try {
				Statement st = c.createStatement();
				int val = st
						.executeUpdate("INSERT INTO UserDetails(name,degree,mobno) VALUES('"
								+ name1
								+ "','"
								+ degree1
								+ "','"
								+ mobno1
								+ "')");
				System.out.println("1 row affected");
			} catch (SQLException ex) {
				System.out.println("SQL statement is not executed!" + ex);
			}
			c.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

MyController.java:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 name = m.getName();
		String degree = m.getDegree();
		String mobno = m.getName();
		InsertDataBase IB = new InsertDataBase();
		IB.insertData1(name, degree, mobno);
		ses.setAttribute("name", name);
		ses.setAttribute("degree", degree);
		ses.setAttribute("mobno", mobno);
		if (name.equals("") || degree.equals("") || mobno.equals("")) {
			return map.findForward("error");
		}
		return map.findForward("success");

	}
}

MyModel.java:

import org.apache.struts.action.ActionForm;

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

	public String getMobno() {
		return mobno;
	}

	public void setMobno(String mobno) {
		this.mobno = mobno;
	}

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

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

	public String getName() {
		return name;
	}
}

Web.xml:


<!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>index.html</welcome-file>
		<welcome-file>index.htm</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:


<?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="MyModel" name="model" />
	</form-beans>
	<action-mappings>
		<action path="/myActionForm" type="MyController" name="model"
			input="/Login.jsp">
			<forward name="success" path="/success.jsp" />
			<forward name="error" path="/fail.jsp" />

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

OUTPUT:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DOWNLOAD+SOURCECODE


Struts with jdbc Integration Tutorial with example program

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply