/* Three classes to build HTML entities from information retrieved * from a database. */ import java.sql.*; import java.util.*; /* This class builds a text input field */ public class HtmlTextFieldFactory { /** Creates a text field. * * @param name The name of the text field. * @param value The text field's current value. */ public static String getTextField(String name, String value) { StringBuffer sb = new StringBuffer(); sb.append("<INPUT TYPE=\"text\" NAME=\""); sb.append(name + "\" "); if (value != null) { sb.append("VALUE = \"" + value + "\""); } sb.append(">"); return sb.toString(); } } /* * This class creates an HTML <SELECT> object based on * the data in an SQLTable. */ public class HtmlSelectListFactory { /** * Creates the <SELECT> list. * * @param conn A JDBC connection. * @param st The SQLTable to pull data out of. * @param selected_id The currently selected id, if any. */ static String getSelectList(Connection conn, SQLTable st, String selected_id) throws Exception { StringBuffer sb = new StringBuffer(); /* Generate the name for the control's parameter. */ String parm_name = st.getPrimaryKey().toLowerCase(); /* Start the SELECT element. */ sb.append("<SELECT NAME=\"" + parm_name +"\">\n"); /* Add a default option with a null value */ sb.append("<OPTION VALUE=\"\">[All]\n"); /* Traverse the result set (may throw an SQLException) * and add the remaining <OPTION> elements. */ ResultSet rs = st.fetchRows(conn); while (rs.next()) { /* I'll inspect the value of selected_id to * determine if the remote user has already * selected something. If so, I'll add the * SELECTED attribute. */ String selected = ""; String id = rs.getString(st.getPrimaryKey()).trim(); if (selected_id != null && selected_id.equals(id)) { selected = "SELECTED"; } sb.append("<OPTION " + selected + " VALUE=" + id + ">"); /* Although I need the primary key for the * value field, I don't want it in the output. * So, I'm using the convenient getDisplayData() * method to return all column values that aren't * primary keys. Then, I just concatenate them * all together. */ Enumeration dd = st.getDisplayData(rs); while (dd.hasMoreElements()) { sb.append((String) dd.nextElement()); sb.append(" "); } sb.append("\n"); } /* Close the statement. */ rs.getStatement().close(); /* Close the SELECT element. */ sb.append("\n</SELECT>"); return sb.toString(); } } /* This class takes the data from an instance of SQLTable * and formats it as an HTML table, headers and all. */ public class HtmlTableFactory { /** * Generate the table. * * @param conn A JDBC connection. * @param st The SQLTable to pull data out of. */ public static String getFormattedTable(Connection conn, SQLTable st) throws Exception { StringBuffer sb = new StringBuffer(); sb.append("<TABLE BORDER>\n"); /* Retrieve the result set. */ ResultSet rs = st.fetchRows(conn); ResultSetMetaData rmd = rs.getMetaData(); /* Enumerate the output columns, and fetch their * descriptions. Then, stuff those descriptions * into table header (TH) elements. */ for(int i = 0; i < rmd.getColumnCount(); i++) { String column = rmd.getColumnName(i + 1); sb.append("<TH>" + st.findColumnDescription(column) + "</TH>"); } sb.append("\n"); // keep it human-readable. /* Traverse the result set (may throw * a SQLException). */ while (rs.next()) { sb.append("<TR>"); for(int i = 0; i < rmd.getColumnCount(); i++) { String column = rmd.getColumnName(i + 1); sb.append("<TD>" + rs.getString(column) + "</TD>"); } sb.append("</TR>\n"); } rs.getStatement().close(); sb.append("\n</TABLE>"); return sb.toString(); } }