Java SQL ResultSet Print printResultSet(ResultSet rs)

Here you can find the source of printResultSet(ResultSet rs)

Description

print Result Set

License

Open Source License

Declaration

public static String printResultSet(ResultSet rs) 

Method Source Code

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

import java.io.PrintStream;

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

public class Main {

    public static void printResultSet(ResultSet rs, PrintStream writer) {
        String print = printResultSet(rs);
        writer.append(print);// ww w.  j  a v  a 2  s  . com
        writer.flush();
    }

    public static String printResultSet(ResultSet rs) {
        StringBuilder sb = new StringBuilder();
        try {
            ResultSetMetaData rsMeta = rs.getMetaData();
            int colCount = rsMeta.getColumnCount();
            int count = 0;
            while (rs.next()) {
                sb.append(count++);
                sb.append("-------------------------------------------------------\r\n");
                for (int i = 1; i <= colCount; i++) {
                    sb.append(rsMeta.getColumnLabel(i) + " : " + "\t\t\t"
                            + rs.getString(i) + "\r\n");
                }
            }
            sb.append("------------------------------------------------------\r\n");
        } catch (SQLException ex) {

            sb.append("***************************************\r\n");
            sb.append("* SQLException in outputResultSet: "
                    + ex.getMessage() + "\r\n");
            sb.append("* SQLState : " + ex.getSQLState() + "\r\n");
            sb.append("* SQL ERROR CODE " + ex.getErrorCode() + "\r\n");
            sb.append("***************************************\r\n");

        }
        return sb.toString();
    }
}

Related

  1. printResults(ResultSet rst)
  2. printResultSet(List> rows, List colNames)
  3. printResultSet(ResultSet rs)
  4. printResultSet(ResultSet rs)
  5. printResultSet(ResultSet rs)
  6. printResultSet(ResultSet rs)
  7. printResultSet(ResultSet rs)
  8. printResultSetColumns(ResultSet rs)
  9. printResultSetHeader(ResultSet rs)