Accessing Relational Databases:- Model Components in Struts Frameworks

Accessing Relational Databases:- Model Components in Struts Frameworks

Previous Home Next

 

Accessing Relational Databases:- Model Components in Struts Frameworks

The model components in struts1.3 framework provide the facility to developed the business logic bean ,it means that struts1.3 framework provide the facility to insert data, fetch data, update data and delete data in database using JDBC API in java application development. It also provide the facility to plug in ORM framework to persist data, fetch data, update data and delete data in your database. 

This tutorial provide the facility to accessing (Fetch all data) which are present in your database table and iterate on your web browser.

 The following source code which are connect to the database and select (fetch data on the web browser) all data fetch which are present in the database table. The following source code using the MySql database connection using JDBC API in java platform. 

Directory Structure of AccessingDatabaseExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<html:link action="/accessDataAction">
Accessing Database Example
</html:link>
</body>
</html>

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>
  <display-name>Maven Struts Examples</display-name> 
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans />
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  <action path="/accessDataAction" type="org.r4r.struts.AccessDataAction">
  <forward name="success" path="/success.jsp"/>
  </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

AccessDataAction.java

package org.r4r.struts;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 AccessDataAction extends Action {
	DAO dao;
	ArrayList<AccessData> dataList;
	public ActionForward execute(ActionMapping mapping,
		ActionForm form,HttpServletRequest request,HttpServletResponse response)
		throws Exception{
		dao=new DAO();
		dataList=dao.find();
		request.setAttribute("dataList",dataList);
		return mapping.findForward("success");
	}
	public ArrayList<AccessData> getDataList() {
		return dataList;
	}
	public void setDataList(ArrayList<AccessData> dataList) {
		this.dataList = dataList;
	}
}

AccessData.java

package org.r4r.struts;
public class AccessData{
	private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public AccessData(String name, String password) {
		super();
		this.name = name;
		this.password = password;
	}
	public AccessData() {
		super();
		// TODO Auto-generated constructor stub
	}
}

DAO.java

package org.r4r.struts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class DAO {	
	public ArrayList<AccessData> find(){
	try{
		Class.forName("com.mysql.jdbc.Driver");
		Connection con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");
		PreparedStatement stmt=con.prepareStatement("select * from user");
		ResultSet rset=stmt.executeQuery();
		ArrayList<AccessData> list=new ArrayList<AccessData>();
		while(rset.next()){
			list.add(new AccessData(rset.getString(1),rset.getString(2)));
		}
		return list;		
	}catch(Exception e){
		System.out.println(e);
	}
	return null;
}
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
label.title=Struts 1.3 Accessing Data Using Database Example
label.header=Struts 1.3 Accessing Data Using Database Example

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<h4><logic:iterate id="accessData" name="dataList">
<ul><li><bean:write name="accessData" property="name"/>
---<bean:write name="accessData" property="password"/></li></ul>
</logic:iterate>
</h4>
</body>
</html>

Output


Previous Home Next