ejb@glassfish.java.net

Re: ArrayList /LinkedList with a table

From: Mahesh.Kannan <Mahesh.Kannan_at_Sun.COM>
Date: Mon, 16 Apr 2007 11:42:35 -0700

If Iteamdetailhlp has methods like getItemId(), getItemName() etc. (just
like Itemdetailsent), then you can use

iteamdetailhlp=iteamdetails.DisCountitems();
for (ItemDetailHlp item : itemdetailhlp) {
    out.println("<tr>");
           out.println("<td>"); out.println(item.getItemId());
out.println("</td>");
           out.println("<td>"); out.println(item.getItemName());
out.println("</td>");
           ...
   out.println(</tr>");
}

OR just modify your toString() to return the values enclose within
"<td> ... "</td>

I would rather use the for loop approach as using HTML tags inside
toString() is not that elegant (particularly if you are planning to use
this toString() from a non-servlet like client)




Eve Pokua wrote:

> Hello,
>
> I have successfully retrieve data from a Database to which I'm using a
> LinkedList.
> The data is being displayed in a servlet but I would like to display
> it in a table with all the columns.
>
> The following is what I have so far.
>
> The romete interface defines my method as:
>
> java.util.List DisCountitems() throws Exception;
>
>
> I have a helper class called:
>
> Iteamdetailhlp
>
> which have the toString method of how the data is displayed.
>
> public String toString() {
> String s = itmid + " " + itmname +" " + description + " " +
> colour + " , ";
>
> return s;
> }
>
> NOw inside my implementation class, I have these two methods:
>
> public List<Iteamdetailhlp> DisCountitems(){
> List<Iteamdetailsent> iteamdetailsent=null;
>
> try{
>
> iteamdetailsent=(List<Iteamdetailsent>)em.createNamedQuery("GetAlldiscountitems").getResultList();
>
>
> return copyToDetails(iteamdetailsent);
> }catch (Exception ex) {
> throw new EJBException(ex);
> }
>
> the above method contains the query to get the data.
>
> private List<Iteamdetailhlp> copyToDetails(List<Iteamdetailsent>
> iteamdetailsents) {
> List<Iteamdetailhlp> detailsList = new
> LinkedList<Iteamdetailhlp>();
>
> Iterator<Iteamdetailsent> i =
> iteamdetailsents.iterator();
> while (i.hasNext()) {
> Iteamdetailsent iteamdetailsent =
> (Iteamdetailsent) i.next();
> Iteamdetailhlp iteamdetailhlp=new
> Iteamdetailhlp(
>
> iteamdetailsent.getitmid(),
> iteamdetailsent.getitmname(),
>
> iteamdetailsent.getdescription(),
> iteamdetailsent.getcolour());
> detailsList.add(iteamdetailhlp);
> }
>
> return detailsList;
> }
>
> The above methods gets the data from DisCountitems() and store it into
> a LinkedList
>
> the Iteamdetailsent is my entity class which has my query as:
>
> @NamedQuery(name = "GetAlldiscountitems",query = "SELECT i FROM
> Iteamdetailsent i where i.Discount='yes'"),
>
> Basically it is retrieving all items which are on a discount.
>
> Now in my servlet I make a reference to the helper class as follows:
>
> List<Iteamdetailhlp> iteamdetailhlp;
> iteamdetailhlp=iteamdetails.DisCountitems();
> out.println("<td>");
> out.println(iteamdetailhlp);
>
> which displays the data as:
>
> [5468679 iris table red , , 564646 iris spoon silver, ]
>
> So that is the item number, name, description and colour.
>
> Now I would rather that it is display as:
>
> ItemID Name Description Colour
> 5468679 iris table red
> 564646 iris spoon silver
>
> If I was retrieving the data without ArrayList or LinkedList, I would
> be calling these as follows
> iteamdetailsent.getitmid(),
> iteamdetailsent.getitmname(),
>
> iteamdetailsent.getdescription(),
> iteamdetailsent.getcolour());
>
> And I can easily put that into a table. I have search for this but
> not got anywhere.
> Please, can you help?
>
> Thanks
>
> eve
>
> _________________________________________________________________
> Txt a lot? Get Messenger FREE on your mobile.
> https://livemessenger.mobile.uk.msn.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ejb-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: ejb-help_at_glassfish.dev.java.net
>