Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 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

constructing an html table from db output in a particular order

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ++;
}
 
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry the posting of code didn't maintain the indentations for readability...
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
try this

didn't test it, but should be along the lines of what your looking for.
Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic