Java SQL ResultSet Print printResultSet(List> rows, List colNames)

Here you can find the source of printResultSet(List> rows, List colNames)

Description

print Result Set

License

Open Source License

Declaration

public static String printResultSet(List<List<String>> rows, List<String> colNames) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static String printResultSet(List<List<String>> rows, List<String> colNames) {
        StringBuffer out = new StringBuffer();
        char delim = '\t';

        //        out.append("Row");
        for (String name : colNames) {
            out.append(name).append(delim);
        }/*from w  w  w  .jav a2  s. c om*/
        if (out.length() > 0)
            out.delete(out.length() - 1, out.length());

        out.append('\n');

        //        int rowCounter = 1;
        for (List<String> row : rows) {
            //            out.append(rowCounter + ".");
            for (String s : row) {
                out.append(s).append(delim);
            }
            if (out.length() > 0)
                out.delete(out.length() - 1, out.length());
            out.append('\n');
            //            rowCounter += 1;
        }

        return out.toString();
    }

    public static String printResultSet(ResultSet rs) throws SQLException {
        return printResultSet(getRows(rs), getColumnNames(rs.getMetaData()));
    }

    /**
     * For SciDB, use getRowsSciDB instead
     * This does not work with SciDB because for SciDB's JDBC reference to "getObject" invariably returns null. 
     * @param rs
     * @return
     * @throws SQLException
     */
    public static List<List<String>> getRows(final ResultSet rs) throws SQLException {
        if (rs == null) {
            return null;
        }
        List<List<String>> rows = new ArrayList<>();
        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            int NumOfCol = rsmd.getColumnCount();
            while (rs.next()) {
                List<String> current_row = new ArrayList<String>();
                for (int i = 1; i <= NumOfCol; i++) {
                    Object value = rs.getObject(i);
                    if (value == null) {
                        current_row.add("null");
                    } else {
                        current_row.add(value.toString());
                    }
                }
                rows.add(current_row);
            }
            return rows;
        } catch (SQLException e) {
            throw e;
        }
    }

    public static List<String> getColumnNames(final ResultSetMetaData rsmd) throws SQLException {
        List<String> columnNames = new ArrayList<String>();
        for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
            columnNames.add(rsmd.getColumnLabel(i));
        }
        return columnNames;
    }
}

Related

  1. printoutDB(ResultSet rs)
  2. printRecord(PrintWriter pw, ResultSet rs)
  3. printResult(ResultSet rs)
  4. printResults(final ResultSet results)
  5. printResults(ResultSet rst)
  6. printResultSet(ResultSet rs)
  7. printResultSet(ResultSet rs)
  8. printResultSet(ResultSet rs)
  9. printResultSet(ResultSet rs)