• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Retrieve objects from ArrayList

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi,
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!