Java SQL ResultSet to resultSetToHtmlTable(java.sql.ResultSet rs)

Here you can find the source of resultSetToHtmlTable(java.sql.ResultSet rs)

Description

result Set To Html Table

License

Apache License

Declaration

public static String resultSetToHtmlTable(java.sql.ResultSet rs) throws SQLException 

Method Source Code

//package com.java2s;
/*//from w  w w  .  j a  v a2 s  .c o m
 * Copyright(c) 2012 Saarland University - Information Systems Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    public static String resultSetToHtmlTable(java.sql.ResultSet rs) throws SQLException {
        int rowCount = 0;
        final StringBuilder result = new StringBuilder();
        result.append("<P ALIGN='center'>\n<TABLE BORDER=1>\n");
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        //header
        result.append("\t<TR>\n");
        for (int i = 0; i < columnCount; ++i) {
            result.append("\t\t<TH>").append(rsmd.getColumnLabel(i + 1)).append("</TH>\n");
        }
        result.append("\t</TR>\n");
        //data
        while (rs.next()) {
            ++rowCount;
            result.append("\t<TR>\n");
            for (int i = 0; i < columnCount; ++i) {
                String value = rs.getString(i + 1);
                if (rs.wasNull()) {
                    value = "&#060;null&#062;";
                }
                result.append("\t\t<TD>").append(value).append("</TD>\n");
            }
            result.append("\t</TR>\n");
        }
        result.append("</TABLE>\n</P>\n");
        return result.toString();
    }
}

Related

  1. resultSet2Map(ResultSet rs)
  2. resultSetAsCSV(ResultSet rsh)
  3. resultSetCurrentData(ResultSet rs)
  4. resultSetParseVO(ResultSet rs, Class cls)
  5. resultSetToArrayList(ResultSet rs)
  6. resultSetToMap(Map mappedValues, java.sql.ResultSet rs, int key, int value)
  7. resultSetToMap(Map mappedValues, ResultSet rs)
  8. resultSetToOrderedMap(ResultSet rs)
  9. resultSetToSet(PreparedStatement statement)