Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Back ground color in Table rows, while using iterator

 
shiva valanukonda
Greenhorn
Posts: 26
Hibernate jQuery Tomcat Server
  • Mark post as helpful
  • send pies
  • 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('');
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65226
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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!

 
Vinod Sekhar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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>");
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or to give yourself more scope to change thing in the future, do it with CSS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic