Java SQL ResultSet to resultSetAsCSV(ResultSet rsh)

Here you can find the source of resultSetAsCSV(ResultSet rsh)

Description

Return a resultset as a csv formatted string with control chars removed.

License

Open Source License

Parameter

Parameter Description
rsh a parameter

Exception

Parameter Description
SQLException an exception
IOException an exception

Declaration

public static String resultSetAsCSV(ResultSet rsh) throws SQLException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2016, Nest NZ/* w ww  .j av a 2s .c om*/
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.sql.*;

import java.io.IOException;

public class Main {
    /**
     * Return a resultset as a csv formatted string with control chars removed.
     * @param rsh
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    public static String resultSetAsCSV(ResultSet rsh) throws SQLException, IOException {
        StringBuilder sb = new StringBuilder();
        ResultSetMetaData rsmd = rsh.getMetaData();
        int numColumns = rsmd.getColumnCount();
        String colName1 = rsmd.getColumnName(1).replace("\"", "").replace(",", "").trim();
        colName1 = colName1.substring(0, 1).toUpperCase() + ((colName1.length() > 0) ? colName1.substring(1) : "");
        String colNames = "\"" + colName1 + "\"";
        for (int i = 2; i < numColumns + 1; i++) {
            String colName = rsmd.getColumnName(i).replace("\"", "").replace(",", "").trim();
            colName = colName.substring(0, 1).toUpperCase() + ((colName.length() > 0) ? colName.substring(1) : "");
            colNames += ",\"" + colName + "\"";
        }
        sb.append(colNames).append("\n");
        System.out.println(sb.toString());
        while (rsh.next()) {
            String row = "\"" + rsh.getString(1).replace("\"", "").replace(",", "") + "\"";
            for (int i = 2; i < numColumns + 1; i++) {
                String val = rsh.getString(i);
                row += ",\"" + ((val == null) ? "" : val).replace("\"", "").replace(",", "") + "\"";
            }
            sb.append(row).append("\n");
            System.out.println(row);
        }
        System.out.println(sb.toString());
        return sb.toString();
    }
}

Related

  1. convertResultSetToJSON(ResultSet resultSet)
  2. convertResultSetToMap(final ResultSet rs)
  3. convertToMap(Map metaData, ResultSet rs)
  4. resultSet2List(ResultSet rs)
  5. resultSet2Map(ResultSet rs)
  6. resultSetCurrentData(ResultSet rs)
  7. resultSetParseVO(ResultSet rs, Class cls)
  8. resultSetToArrayList(ResultSet rs)
  9. resultSetToHtmlTable(java.sql.ResultSet rs)