• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to format this table in jsp page?  RSS feed

 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table in database and there are four data field in it.They are "id","name","email","registetime".
In javabean,I add all this data to a vector:" ResultSet rs;
while(rs.next()){
vec.add(rs.getString("id");
vec.add(rs.getString("name");
vec.add(rs.getString("email");
vec.add(rs.getString("registetime");} ";
I use a Iterator to read all the data to jsp page:"
iter=vec.iterator();
while(iter.hasNext()){
int i=0;
%>
<table border="1" bordercolor="#FF6699" bgcolor="#CCCC99">
<%
if(i==0|i%8==0){
out.println("<td><tr>");
}
%>
<%=iter.next()%>
</td>
<%
if(i%8==0)
out.println("</tr></td>");
i++;
%></table>
I use a variable i for change range.I want to output a '\n' after output 4 data.But now the data in IE is this:
///////////////////////////////////////////
1
lyo
lyo@lyo.com
2003-6
2
Yashnoo
Yashnoo@Yashnoo.com
2003-7
/////////////////////////////////////////////
It not that I want to get.I means I want to get data like this:
/////////////////////////////////////////////////
1 lyo lyo@lyo.com 2003-6
2 Yashnoo Yashnoo@Yashnoo.com 2003-7
...... ........ ........ ...... ......
/////////////////////////////////////////////////
How can I do?
 
kavitha rama
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You defined <td><tr> which should be other way. <tr> contains <td>. Try with it. In a row, use <td> for each field. Hope that helps
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.But I change it to this:
////////////////////////////////////////////
<body>
<table border="1" bordercolor="#FF6699" bgcolor="#CCCC99">
<%
Iterator iter=null;
ResultSet rs=null;
Vector vec=new Vector();
vec=lyo.addData(rs,vec);
iter=vec.iterator();
try{
while(iter.hasNext()){
int i=0;
%>
<%
if(i%8==0){
out.print("<tr>");
}
out.print("<td>");
}
%>
<%=iter.next()%>
<%

out.print("</td>");
if(i%8==0){
out.print("</tr>");
}
i++;
%></table>
<%
}
}catch(Exception e){
out.println(e.toString());
}
%>
It can't help? Why? :roll:
 
chakri
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi ,
This will work.
<%
------ Your code to fill vector --------
Iterator iter=vec.iterator();
int i=0;
%>
<table>
<%
try{
while(iter.hasNext())
{
if(i%4==0)
{
out.print("<tr>");
}
out.print("<td>");
out.print(iter.next());
out.print("</td>");
i++;
if(i%4==0)
{
out.print("</tr>");
}
}
%>
</table>
<%
}catch(Exception e)
{
out.println(e.toString());
}
%>
 
Shrikant Chopade
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need Iterator? I mean you can retrieve data from ResultSet directly and put it in <td> of the table.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!