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 void printResultSet(ResultSet rs) throws SQLException 

Method Source Code

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

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

public class Main {
    public static void printResultSet(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        int numColumns = printResultSetHeader(metaData);

        while (rs.next()) {
            printResultSetRow(rs, numColumns);
        }/*  w ww.jav a2  s  .c om*/
    }

    public static int printResultSetHeader(ResultSet rs)
            throws SQLException {
        return printResultSetHeader(rs.getMetaData());
    }

    public static int printResultSetHeader(ResultSetMetaData metaData)
            throws SQLException {
        int numColumns = metaData.getColumnCount();
        for (int i = 1; i <= numColumns; i++)
            System.out.print(metaData.getColumnLabel(i) + "\t");
        System.out.println("");
        return numColumns;
    }

    public static void printResultSetRow(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        int numColumns = printResultSetHeader(metaData);

        printResultSetRow(rs, numColumns);
    }

    public static void printResultSetRow(ResultSet rs, int numColumns)
            throws SQLException {
        Object obj = null;
        for (int i = 1; i <= numColumns; i++) {
            obj = rs.getObject(i);
            if (obj != null) {
                System.out.print(rs.getObject(i).toString() + "\t");
            } else {
                System.out.print("\t\t");
            }
        }
        System.out.println("");
    }
}

Related

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