Java SQL ResultSet Print print(ResultSet rs, PrintStream out)

Here you can find the source of print(ResultSet rs, PrintStream out)

Description

Prints a ResultSet.

License

Open Source License

Parameter

Parameter Description
rs The <code>ResultSet</code> to print.
out The <code>PrintStream</code> to write to.

Exception

Parameter Description
SQLException If an error occurs while reading the<code>ResultSet</code>.

Declaration

public static void print(ResultSet rs, PrintStream out) throws SQLException 

Method Source Code


//package com.java2s;
/*/*from   w  ww .j  a  va  2s .c  o m*/
 * Copyright (c) 2008 Bradley W. Kimmel
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.PrintStream;

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

public class Main {
    /**
     * Prints a <code>ResultSet</code>.
     * @param rs The <code>ResultSet</code> to print.
     * @param out The <code>PrintStream</code> to write to.
     * @throws SQLException If an error occurs while reading the
     *     <code>ResultSet</code>.
     */
    public static void print(ResultSet rs, PrintStream out) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        int columns = meta.getColumnCount();
        for (int i = 0; i < columns; i++) {
            if (i > 0) {
                out.print(",");
            }
            out.print("\"");
            out.print(meta.getColumnName(i + 1));
            out.print("\"");
        }
        out.println();

        while (rs.next()) {
            for (int i = 0; i < columns; i++) {
                if (i > 0) {
                    out.print(",");
                }
                String value = rs.getString(i + 1);
                if (value != null) {
                    out.print("\"");
                    out.print(value.replaceAll("\"", "\"\""));
                    out.print("\"");
                } else {
                    out.print("NULL");
                }
            }
            out.println();
        }
    }
}

Related

  1. prettyFormat(ResultSet res)
  2. print(ResultSet resultSet)
  3. print(ResultSet rs, PrintStream out)
  4. printCols(final ResultSetMetaData paymentTableResultSetMetaData, final String logging_prefix)
  5. printColTypes(ResultSetMetaData rsmd)
  6. printHeader(PrintWriter pw, ResultSet rs)