Java SQL ResultSet Dump dumpRs(ResultSet rs, PrintWriter out, String delimiter, boolean printTrailingDelimiter)

Here you can find the source of dumpRs(ResultSet rs, PrintWriter out, String delimiter, boolean printTrailingDelimiter)

Description

dump Rs

License

Open Source License

Parameter

Parameter Description
rs a parameter
out a parameter
delimiter a parameter
printTrailingDelimiter a parameter

Exception

Parameter Description

Declaration

public static void dumpRs(ResultSet rs, PrintWriter out, String delimiter, boolean printTrailingDelimiter)
        throws SQLException 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * Copyright (C) 2008 EnterpriseDB Corporation.
 * Copyright (C) 2011 Stado Global Development Group.
 *
 * This file is part of Stado.//from  w w  w  .  j  a  v  a2 s  .c o  m
 *
 * Stado is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Stado is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Stado.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can find Stado at http://www.stado.us
 *
 ****************************************************************************/

import java.io.PrintWriter;

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

public class Main {
    /**
     *
     * @param rs
     * @param out
     * @param delimiter
     * @param printTrailingDelimiter
     * @throws java.sql.SQLException
     */
    public static void dumpRs(ResultSet rs, PrintWriter out, String delimiter, boolean printTrailingDelimiter)
            throws SQLException {

        if (!rs.next()) {
            out.println("no rows to display");
            out.flush();
            return;
        }

        java.sql.ResultSetMetaData meta = rs.getMetaData();
        int c = meta.getColumnCount();
        int types[] = new int[c];

        for (int i = 0; i < c; i++) {
            types[i] = meta.getColumnType(i + 1);
            out.print(meta.getColumnLabel(i + 1).toUpperCase() + delimiter);
            // out.print(meta.getColumnName(i+1) + " - " +
            // meta.getColumnClassName(i+1) + delimiter);
        }

        out.println();
        int rowsCnt = 0;

        // java.text.DecimalFormat aDF = new java.text.DecimalFormat();
        // aDF.setGroupingUsed(false);

        // write rows
        do {
            for (int i = 0; i < c; i++) {
                String value = rs.getString(i + 1);
                out.print((value == null ? "null" : value.toString().trim()));
                if (printTrailingDelimiter || i < c - 1) {
                    out.print(delimiter);
                }
            }
            out.println();
            out.flush();
            rowsCnt++;
        } while (rs.next());
        out.println(rowsCnt + " rows dumped");
        out.flush();
    }
}

Related

  1. dumpResultSet(ResultSet dbResult)
  2. dumpResultSet(ResultSet resultSet, String tablename, String[] columns)
  3. dumpResultSet(ResultSet rs)
  4. dumpResultSet(ResultSet rs)
  5. dumpResultSetMetaData(ResultSet dbResult)