Friday, December 24, 2010

how to retrive table data in struts

Action class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 tableAction extends Action {
   
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        try{
            tableForm data = (tableForm) form;
            String s[]=null;
            String uname=data.getUsername();
            String pas=data.getPassword();
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","tiger");
            PreparedStatement ps1=con.prepareStatement("insert into stdval values(?,?)");
            ps1.setString(1,uname);
            ps1.setString(2,pas);
            int i=ps1.executeUpdate();
            if(i==1){
                System.out.println("insert");
            }else{
                System.out.println("fail");
            }
            PreparedStatement ps=con.prepareStatement("select * from stdval");
            ResultSet rs=ps.executeQuery();
            /*ResultSetMetaData rsmd=rs.getMetaData();
            int no=rsmd.getColumnCount();
            for(int count=1;count<=no;count++){
                s[count]=rsmd.getColumnName(count);
            }
            request.setAttribute("count",no);
            request.setAttribute("header",s);*/
            int c=0;
            ArrayList al=new ArrayList();
            while(rs.next()){
                c=1;
                tableForm data1=new tableForm();
                data1.setUsername(rs.getString("use"));
                data1.setPassword(rs.getString("pass"));
                al.add(data1);
            }
            if(c==0){
                return mapping.findForward("fail");
            }else{
                request.setAttribute("res",al);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return mapping.findForward("success");
    }
}
FormBean:
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
import org.apache.struts.action.ActionForm;

/**
 * MyEclipse Struts
 * Creation date: 10-24-2010
 *
 * XDoclet definition:
 * @struts.form name="data"
 */
public class tableForm extends ActionForm {
    /*
     * Generated fields
     */

    /** password property */
    private String password;

    /** username property */
    private String username;

    /*
     * Generated Methods
     */

    /**
     * Returns the password.
     * @return String
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set the password.
     * @param password The password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Returns the username.
     * @return String
     */
    public String getUsername() {
        return username;
    }

    /**
     * Set the username.
     * @param username The username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
}
index.jsp:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

    <head>
       
    </head>
    <body><center>
        <html:form action="/data">
            UserName : <html:text property="username" /><br />
            Password : <html:text property="password" /><br />
            <html:submit />
        </html:form></center>
    </body>

<table border="1" align="center">
    <%
        ArrayList as = (ArrayList) request.getAttribute("res");
       
    %>
    <logic:notEmpty name="res" scope="request">
        <tr bgcolor="red">
            <th>UserName</th>
            <th>Password</th>
        </tr>
        <logic:iterate id="id1" collection="<%=as%>">
            <tr>
                <td><center><bean:write name="id1" property="username"/></center></td>
                <td><center><bean:write name="id1" property="password"/></center></td>
            </tr>
        </logic:iterate>
    </logic:notEmpty>

</table>
failure.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'fail.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
 
  <body>
    This is my JSP page. <br>
  </body>
</html>

0 comments:

Post a Comment