1

hey guys i want to display the data from ResultSet in HTML table .. here is my code

while(result.next()){
            writer.println("<table BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>"
                    + "<tr><th>FIRSTNAME</th><th>LASTNAME</th></tr>"
                    + "<tr><td><center>"+result.getString("firstname")+"</center></td>"
                    + "<td><center>"+result.getString("lastname")+"</center></td></tr>   </table>");
        }

but it foesnot work well for multiple rows .. so any help ??

2
  • 2
    move out of the while the <table> and header declaration? Commented Oct 29, 2014 at 17:34
  • You want one <table>, followed by one <tr> containing <th>s, then N <tr>s containing <td>s, then one </table>. Only the N <tr>s containing <td>s should be in the while loop. That said, you have tagged your question with jsp. The HTML-generating code should be in the JSP, use the JSTL and iterate through a List<Person>, constructed by the servlet from the result set. Commented Oct 29, 2014 at 17:37

3 Answers 3

1

Put the last line outside the loop

Sign up to request clarification or add additional context in comments.

Comments

1

How about something like this?

writer.println("<table BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>"
              +"<tr><th>FIRSTNAME</th><th>LASTNAME</th></tr>");

while(result.next()){
  writer.println("<tr><td><center>"+result.getString("firstname")+"</center></td>"
               + "<td><center>"+result.getString("lastname")+"</center></td></tr>");
}
writer.println("</table>");

1 Comment

for multiple rows it gives me FIRSTNAME and LASTNAME in every row which is not logical
0

Steps to convert ResultSet object to HTML content

(1) Import the package in your JAVA class file

(2) Create an object of class type "HtmlReportGenerator"

(3) Set Table Name as a String

(4) Set Column Names as a comma separated String

(5) Call "toHTMLTable()" method with the ResultSet Object,table name and Column Name as parameters

  Example:

  import frizbee.beach.*;
  //Inside Class file

  HtmlReportGenerator hrp=new HtmlReportGenerator();

  String columnNames="col1,col2,col3,col4,col5,col6,col7,col8,col9"; 

  String tableName="abcde"; 

  String html=hrp.toHTMLTable(rs,tableName,columnNames); 

  System.out.println(html);

@SuppressWarnings("unchecked")
public String toHTMLTable(ResultSet rs,String tableName,String columnNames) throws JSONException, SQLException {

    ResultSetMetaData rsmd = rs.getMetaData();
    int numColumns = rsmd.getColumnCount();

    JSONArray json = new JSONArray();                                               
    while(rs.next()) {
        LinkedHashMap<String, Object> lhm = new LinkedHashMap<String, Object>(); 
        for (int i=1; i<=numColumns; i++) {
            String column_name = rsmd.getColumnLabel(i);
            //String value= rs.getObject(column_name);
            lhm.put(column_name,rs.getObject(column_name));
        }
     json.put(lhm);
    }



    String[] colNames=columnNames.split(",");
    StringBuilder s1=new StringBuilder();
    s1.append("<div style=\"font-size:14pt;font-weight:bold;margin:15pt auto;text-align:center\">"+tableName+"</div>"
            +       " <table style=\"background:white;border-collapse: collapse;text-align:center;margin:10pt auto;border-radius:4px;border:1px solid rgba(255, 30, 0, 0);width:97%\" width=\"97%\" align=\"center\">" + 
            "       <thead style=\"background:#a6a6a6;color:white\">");
    for(int i=0;i<colNames.length;i++) {
        s1.append("<th style=\"padding:10pt 2pt;\">"+colNames[i]+"</th>");
    }
    s1.append("</thead><tbody>");


    for (int i = 0; i < json.length(); i++) {
        s1.append("<tr>");
        JSONObject jsonObj = json.getJSONObject(i);
        Iterator<String> keys = jsonObj.keys();

        while (keys.hasNext()) {

            String key = keys.next();
            String val="";
            if(jsonObj.isNull(key)) {
                val="";
            }

            else {

                val= jsonObj.get(key).toString();
            }
            s1.append("<td style=\"padding:7pt 2pt;border-bottom: 1px solid black !important;"+bgColor(i)+"\">"+val+"</td>");
        }
        s1.append("</tr>");
    }
    s1.append("</tbody></table>");

    return s1.toString();
}


private  String bgColor(int i) {
    return i%2==0?"background-color:#ededed":"";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.