tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Amit Gupta" <>
Subject RE: Newbie question on Database and struts
Date Fri, 24 Dec 2004 04:05:34 GMT
Following code fetch records from database table , populate them to hastable and create vector
of these hashtables . Then we pass this vector to JSP through request attribute.

Action class
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.beanutils.*;
public class viewWsForkliftsAction extends org.apache.struts.action.Action {

    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {
        	HttpSession session=request.getSession();
        		request.setAttribute("username", session.getAttribute("username"));
        javax.sql.DataSource dataSource;
 		Connection conn;
		try {
	  			dataSource = getDataSource(request);
  				conn = dataSource.getConnection();
  				Statement stmt = null;
				ResultSet rs = null;
 String sqlquery="select * from my_table";
 sqlquery=sqlquery+"and dcc_forklifts.status=\"Active\"";
				stmt = conn.createStatement();
			    rs= stmt.executeQuery(sqlquery);
			    ResultSetMetaData rsmd = rs.getMetaData();
    			int numberOfColumns = rsmd.getColumnCount();
				Vector rows=new Vector();
			    	Hashtable  fields=new Hashtable();
					for(int j=1; j<=numberOfColumns;j++)				
				return mapping.findForward(request.getParameter("page"));
			catch (SQLException sqle) 
     			return mapping.findForward("sqlexception");	

In JSP page , I access this vector as

Vector rows =(Vector)request.getAttribute("rows");

int sz=rows.size();
for(int i=0;i< sz;i++)
	Hashtable row=(Hashtable)rows.get(i);


With regards,

Amit Gupta
-----Original Message-----
From: Manisha Sathe [] 
Sent: Friday, December 24, 2004 8:55 AM
Subject: Newbie question on Database and struts

I am new to struts (even new to java also)
I have one form developed in struts. What i want to do is depending on form values, run sql
qry in Action form, get result set and display it on JSP.
I have created a databean for the results. But with result set -> How to create array of
databeans ? and how to post it on JSP ?
if anybody can pass me the samples pls would be of great help

Do you Yahoo!?
 Read only the mail you want - Yahoo! Mail SpamGuard.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message