Java SQL ResultSet Print printResultsToFile(ResultSet rst, File outFile)

Here you can find the source of printResultsToFile(ResultSet rst, File outFile)

Description

print Results To File

License

Open Source License

Declaration

public static int printResultsToFile(ResultSet rst, File outFile) throws SQLException, FileNotFoundException 

Method Source Code


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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    public static int printResultsToFile(ResultSet rst, File outFile) throws SQLException, FileNotFoundException {
        //       if(!rst.isBeforeFirst()){
        //          throw new SQLException("Result set not before first");
        //       }
        PrintWriter fileprinter = new PrintWriter(outFile);
        // Print out your results
        ResultSetMetaData meta = rst.getMetaData();
        int numColumns = meta.getColumnCount();
        fileprinter.print(meta.getColumnName(1));
        for (int j = 2; j <= meta.getColumnCount(); j++)
            fileprinter.print(", " + meta.getColumnName(j));
        fileprinter.println();/*from  ww w. ja  v a  2  s .  co  m*/
        int count = 0;
        while (rst.next()) {
            fileprinter.print(rst.getObject(1));
            for (int j = 2; j <= numColumns; j++)
                fileprinter.print(", " + rst.getObject(j));
            fileprinter.println();
            count++;
        }
        fileprinter.close();
        return count;
    }
}

Related

  1. printResultSet(ResultSet rs)
  2. printResultSetColumns(ResultSet rs)
  3. printResultSetHeader(ResultSet rs)
  4. printResultSetMetadata(ResultSetMetaData rsmd, Writer out)
  5. printResultSetRow(ResultSet rs)
  6. printRow(ResultSet pk)
  7. printRsetTypeAndConcurrencyType(ResultSet rset)
  8. printSet(ResultSet set, int columns)
  9. printValues(final ResultSet resultSet)