Skip to content Skip to sidebar Skip to footer

Displaying Data From Resultset In Html Table

hey guys i want to display the data from ResultSet in HTML table .. here is my code while(result.next()){ writer.println('

Solution 1:

Put the last line outside the loop

Solution 2:

How about something like this?

writer.println("<tableBORDER=1CELLPADDING=0CELLSPACING=0WIDTH=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>");

Solution 3:

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 fileHtmlReportGenerator hrp=newHtmlReportGenerator();

  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")
publicStringtoHTMLTable(ResultSet rs,String tableName,String columnNames) throws JSONException, SQLException {

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

    JSONArray json = newJSONArray();                                               
    while(rs.next()) {
        LinkedHashMap<String, Object> lhm = newLinkedHashMap<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=newStringBuilder();
    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();
}


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

Post a Comment for "Displaying Data From Resultset In Html Table"