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();
if(session.getAttribute("username")!=null)
{
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();
while(rs.next())
{
rs=stmt.getResultSet();
Hashtable fields=new Hashtable();
for(int j=1; j<=numberOfColumns;j++)
{
fields.put(rsmd.getColumnName(j),rs.getString(rsmd.getColumnName(j)));
}
rows.add(fields);
}
request.setAttribute("rows",rows);
conn.close();
return
mapping.findForward(request.getParameter("page"));
}
catch (SQLException sqle)
{
request.setAttribute("val",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 [mailto:[EMAIL PROTECTED]
Sent: Friday, December 24, 2004 8:55 AM
To: [email protected]
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
regards
Manisha
---------------------------------
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.