• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Retrieve objects from ArrayList

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
i have a bean "ResultBean" that saves the information of a territory. I have added that bean to the ArrayList.
Now for writing the XML i want to access the bean's object which is in the ArryList.There was one relevant thread in the forum but that was of little help..
How can i do that??
------------------------
public class SecondOptionsAction extends Action
{
public ArrayList fillDataTable(String sqlQuery )
{
ArrayList results= new ArrayList();
ResultSet rs=null;
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:ankitDB1","scott","tiger");
Statement st=con.createStatement();
rs= st.executeQuery(sqlQuery);
while (rs.next())
{
//ArrayList results= new ArrayList();
ResultBean resultBean = new ResultBean();

resultBean.setTerritoryDescription( rs.getString("TerritoryDescription") );
resultBean.setRegionid(rs.getString("regionid"));
resultBean.setTerritoryID(rs.getString("TerritoryID"));

results.add(resultBean);
system.out.println("results added in Bean");
}
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
return results;
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//-----------added from vb file
response.setContentType("text/xml");
String strQuery="";
// 'q' is the selected Region='Eastern'
strQuery = request.getParameter("q");
String strForm="";
// 'f' is the name of the form
strForm = request.getParameter("f");
String strElem="";
// 'e' is the second select (teritory)in the jsp= 'assam'
strElem = request.getParameter("e");

String strSql = "SELECT TerritoryDescription,TerritoryID FROM Territories"
+ " WHERE regionid ='" + strQuery +"' ORDER BY TerritoryDescription";

ArrayList dtOptions=new ArrayList();
dtOptions=fillDataTable(strSql);

while(!(dtOptions.isEmpty()))
{
Iterator iterator=dtOptions.iterator();
//for(int i=0;i<=dtOptions.size();i++)
while(iterator.hasNext())
{
//How to do it???
//String terr_description=dtOptions[i].getTerritoryDescription();
//String terr_id=dtOptions.get(i).getTerritoryID();
String terr_id=iterator.next();
System.out.println("hello the value is ["+iterator.next()+"]");

StringBuffer strXML= new StringBuffer("<?xml version=1.0 ?>");

strXML.append("<selectChoice>");
strXML.append("<selectElement>");
strXML.append("<formName>"+strForm+"</formName>");
strXML.append("<formElem>"+strElem+"</formElem>");
strXML.append("</selectElement>");

strXML.append("<entry>");
strXML.append("<optionText>Select A Territory</optionText>");
strXML.append("<optionValue>-1</optionValue>");
strXML.append("</entry>");

strXML.append("<entry>");
//strXML.append("<optionText>"+terr_description
//+"</optionText>");
//strXML.append("<optionValue>"+terr_id
//+"</optionValue>");
strXML.append("</entry>");
strXML.append("</selectChoice>");
}
}
return mapping.findForward("index");
}
}
--------------------------

Please help..
Regards,
Roshani
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two ways:

With an Iterator


Or as you might an array
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul and Christian,

Thanks a million!!! .. i cant say anymore!!

Regards,
Roshani..
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

ArrayList get() or Itegrator get method returns the Object, typecast it for correct object and use it.

Typesafe code is :


List list = fillDataTable("select * from table"); Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object obj=iterator.next();
if(obj instanceof ResultBean)
{
ResultBean currentBean = (ResultBean)obj;
// etc.}


This will avoid, Runtime ClassCastExceptions
 
reply
    Bookmark Topic Watch Topic
  • New Topic