• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Back ground color in Table rows, while using iterator

 
Greenhorn
Posts: 26
Hibernate jQuery Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am writing some code which is going to display data from DB using Hibernate pagination concept. Here , i am facing the issue with bg color for table rows. i want to put different colors (first row should be light blue and second row should be light gray color. Here is my code.

-----------------
Session ses=factory.openSession();
Criteria crit1=ses.createCriteria(registergoods.class);
crit1.setProjection(Projections.rowCount());
List l1=crit1.list();
Iterator it1=l1.iterator();
if(it1.hasNext())
{
Object o=it1.next();
totNumberOfRecords=Integer.parseInt(o.toString());
}
Criteria crit2=ses.createCriteria(registergoods.class);
crit2.addOrder(Order.desc("purchasedon"));
crit2.setFirstResult((pageIndex*numberOfRecordsPerPage)-numberOfRecordsPerPage);
crit2.setMaxResults(numberOfRecordsPerPage);
List l2=crit2.list();
Iterator it2=l2.iterator();


Criteria crit3=ses.createCriteria(registergoods.class);
crit3.setProjection(Projections.sum("amount"));

List l3=crit3.list();
Iterator it3=l3.iterator();
while(it3.hasNext())
{
am=(Integer)it3.next();
Amount=Double.parseDouble(Integer.toString(am));
System.out.println("am is ::"+Amount);
}
Double bal=26870.00-Amount;
System.out.println("Balance is :: "+bal);
pw.println("<table width=100% border=0 bgcolor=#0299FF><tr><td width=20%>");
pw.println("<font color=Green size=4 valign=right> Balance: ");
pw.println("</td></font><td width=35%>"+bal);
pw.println("</td>");
pw.println("<td width=35%><font color=Red size=4 valign=right> Purchased Amount as on Date is");
pw.println("</td></font><td width=10%>"+Amount);
pw.println("</td></tr></table><br>");

pw.println("<center><table border=0 width=80%>");
pw.println("<tr bgcolor=brown><td><b>ID</b></td> <td><b>PRODUCT</b></td> <td> <b>QUANTITY</b></td> <td><b>AMOUNT</b></td> <td><b>PURPOSE</b></td> <td><b>PURCHASEDON</b></td> </tr>");
while(it2.hasNext())
{
registergoods rg=(registergoods)it2.next();
pw.println("<tr>");
pw.println("<td><input type=checkbox name=check value="+rg.getId()+"></td>");
//pw.println("<td>"+rg.getId()+"</td>");
pw.println("<td>"+rg.getProduct()+"</td>");
pw.println("<td>"+rg.getQuantity()+"</td>");
pw.println("<td>"+rg.getAmount()+"</td>");
pw.println("<td>"+rg.getPurpose()+"</td>");
pw.println("<td>"+rg.getPurchasedon()+"</td>");
pw.println("</tr>");

}
pw.println("</table>");
pw.println("<input type=submit value=Delete>");
int noOfPages=totNumberOfRecords/numberOfRecordsPerPage;
if(totNumberOfRecords>(noOfPages*numberOfRecordsPerPage))
{
noOfPages=noOfPages+1;
}
pw.println("<br>");
for(int i=1;i<=noOfPages;i++)
{
String url="srv2?pageindex="+i;
pw.println("<b><a href="+url+">"+ " |"+i+"|</a></b>");
}
pw.println("</center>");
ses.close();
pw.close();


-------------------

The bold one should fetch the data from DB using resultset, I want to use a color for the first iteration and other color for second iteration like that i want to use total 2 colors..
so please help me in this
Thanks in advance.
Shiva javascript:emoticon('');
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you creating HTML in a servlet? You should be grabbing the data and forwarding it off to a JSP to render the view.

Rule #1: No Java code in a JSP!
Rule #2: No HTML in a servlet!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
declare some integer variable and keep incrementing with +1 it in while (it2.hasNext()) loop, for example: int i;

change the pw.println("<tr>"); to ( i%2 == 1) ? pw.println("<tr bgcolor=gray>") : pw.println("<tr bgcolor=yellow>");
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or to give yourself more scope to change thing in the future, do it with CSS.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic