Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Back ground color in Table rows, while using iterator  RSS feed

 
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('');
 
Author and ninkuma
Marshal
Posts: 66804
168
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!

 
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>");
 
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
Boost this thread!