Java SQL ResultSet saveResultsetAsFile(ResultSet rs, String filePath, String fileName)

Here you can find the source of saveResultsetAsFile(ResultSet rs, String filePath, String fileName)

Description

save Resultset As File

License

Apache License

Declaration

public static void saveResultsetAsFile(ResultSet rs, String filePath, String fileName) 

Method Source Code


//package com.java2s;
/*//from ww w .j  a  va2 s. c om
 * Copyright 2012 University of Southern California
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.apache.log4j.Logger;

public class Main {
    static private Logger logger = Logger.getLogger("AppLogging");

    public static void saveResultsetAsFile(ResultSet rs, String filePath, String fileName) {
        logger.info("Inside cacheResultSet function");
        //logger.info("The path of the file passed here is"+ filePath);
        logger.info("The name of the file passed here is" + fileName);

        FileWriter out = null;
        try {
            out = new FileWriter(filePath + fileName);
            ResultSetMetaData metaData = rs.getMetaData();
            int numberOfColumns = metaData.getColumnCount();
            /*List<Object> columnNames = new ArrayList<Object>(numberOfColumns);
            // Get the column names
            for (int column = 0; column < numberOfColumns; column++) {
               columnNames.add((metaData.getColumnLabel(column + 1)));
            }
            out.write(columnNames+"\n");*/

            while (rs.next()) {
                for (int i = 1; i <= numberOfColumns; i++) {
                    out.write((rs.getString(i) == null) ? "" : rs.getString(i));
                    if (i != numberOfColumns)
                        out.write("\t");
                }
                out.write("\n");
            }
        } catch (IOException io) {
            logger.error("IOException while writing cache file from resultset : " + io.getMessage());
            io.printStackTrace();
        } catch (SQLException sqle) {
            logger.error("SQL Exception while writing cache file from resultset : " + sqle.getMessage());
            sqle.printStackTrace();
        } catch (Exception e) {
            logger.error("Exception while writing cache file from resultset : " + e.getMessage());
            e.printStackTrace();
        } finally {
            if (out != null) {
                //System.out.println("Closing PrintWriter");
                try {
                    out.flush();
                    out.close();
                    out = null;
                } catch (IOException io) {
                    logger.error("IOException while closing ouputStream for cache resultset : " + io.getMessage());
                    io.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    if (rs.getStatement() != null) {
                        rs.getStatement().close();
                    }
                    rs.close();
                    rs = null;
                } catch (SQLException sqle) {
                    System.err.println("SQL-Exception when closing the statement: " + sqle.getMessage());
                    sqle.printStackTrace();
                }
            }
        }
        logger.info("After saving result to file");
        return;
    }
}

Related

  1. mapResultSet(ResultSet set)
  2. moveToPageNo(ResultSet rs, int pageNo, int pageSize)
  3. parseSqlResultToListMap(ResultSet rs)
  4. putResultSetToMap(final Hashtable map, final ResultSet resultSet, final String... keys)
  5. safeGetString(ResultSet r, String name)
  6. toXML(ResultSet rs)
  7. writeResultSetToFile(String path, ResultSet results)