• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructing an html table from db output in a particular order  RSS feed

 
michael dizon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to construct a table of wheels but cant seem to get them to lay out properly. There are a bunch of wheels that come back in the ResultSet but when put in the table come back all in one big row. I would like to make it so that I can break up the results to make a html table that contains no more than three wheels per row. I'm guessing I would use a vector or something to store data in and then print it out.
PreparedStatement ps = conn.prepareStatement("select distinct image, wheel_name, type from wheels where type = 'truck'");
ResultSet rs = ps.executeQuery();
String table = " ";
String wheel = "";
String image = "";
String type = null;
while (rs.next()){
wheel = rs.getString("wheel_name");
image = rs.getString("image");
type = rs.getString("type");
table += "<td rowspan=\"2\" align=\"center\"><a href=\"wheel.jsp?wheel=" + wheel + "&type=" + type + "\"><img src=\"images/" + image + "\" border=\"0\"></a><br><font face=\"verdana,arial\" size=\"-2\" color=\"yellow\"><b>" + wheel + "</b></font></td>";

count ++;
}
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the following code...
This is a simple bit of code that you pass in a result set and a title to use for the table. It uses the database metadata to discover the column names and uses them for header lables.
public String dumpAnyResultSetToTable(java.sql.ResultSet rs, String title) throws java.sql.SQLException {

StringBuffer sb = new StringBuffer();
sb.append("<TABLE border=1>");

java.sql.ResultSetMetaData rsmd = rs.getMetaData();
final int cols = rsmd.getColumnCount();
sb.append("<tr><td colspan=");
sb.append(cols + ">");
sb.append(title);
sb.append("</td></tr>");
// Load labels and column names...
// Element 0 will not be used to simplify code.
String[] labels = new String[cols + 1];
String[] names = new String[cols + 1];

for (int i = 1; i <= cols; i++) {
labels[i] = rsmd.getColumnLabel(i);
names[i] = rsmd.getColumnName(i);
}

// Build table header
sb.append("<tr>");
for (int i = 1; i <= cols; i++) {

sb.append("<th>");
sb.append(labels[i]);
sb.append("</th>");

}
sb.append("</tr>");

// Build the table data
while (rs.next()) {

sb.append("<tr>");

for (int i = 1; i <= cols; i++) {

sb.append("<td>");
sb.append(rs.getString(names[i]));
sb.append("</td>");
}

sb.append("</tr>");
}

sb.append("</table>");

return sb.toString();

}
Hope it helps and gives you some ideas of your own.
Happy coding,
 
Byron Estes
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the posting of code didn't maintain the indentations for readability...
 
Mike Piotrowski
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
try this

didn't test it, but should be along the lines of what your looking for.
Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!