Pages

Friday, April 8, 2011

Write Comma Separated Value (CSV) from ResultSet

This code requires the use of the free library java CSV and MyCsvWriter.
. . .
MyCsvWriter csvWriter = new MyCsvWriter("File Name.csv");
try {
      csvWriter.writeColumnNames(rs);
      csvWriter.writeRecords(rs);
      csvWriter.flush();
      csvWriter.close();           
} catch (SQLException e) {
      System.out.println("Oops-> " + e.getMessage());
} catch (IOException e) {
      System.out.println("Oops-> " + e.getMessage());
}
. . .


MyCsvWriter :
/**
 * @author Eri Setiawan
 */

import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.csvreader.CsvWriter;

public class MyCsvWriter extends CsvWriter {

      public MyCsvWriter(OutputStream arg0, char arg1, Charset arg2) {
            super(arg0, arg1, arg2);
      }

      public MyCsvWriter(String arg0, char arg1, Charset arg2) {
            super(arg0, arg1, arg2);
      }

      public MyCsvWriter(String arg0) {
            super(arg0);
      }

      public MyCsvWriter(Writer arg0, char arg1) {
            super(arg0, arg1);
      }
      /**
       *
       * @param rs
       * @throws SQLException
       * @throws IOException
       */
      public void writeColumnNames(ResultSet rs) throws SQLException, IOException {
            for(int i = 1;i <= rs.getMetaData().getColumnCount();i++){
                  String columnName = rs.getMetaData().getColumnName(i);
                  String columnLabel = rs.getMetaData().getColumnLabel(i);
                  if(columnLabel.equals(columnName))
                        write(formatColumnName(columnName));
                  else
                        write(formatColumnName(columnLabel));
            }
            endRecord();
      }
      public void writeRecords(ResultSet rs) throws SQLException, IOException{
            while(rs.next()){
                  for(int i = 1;i <= rs.getMetaData().getColumnCount();i++){
                        write(rs.getString(i));
                  }
            }
      }
     
      /**
       *  Convert an unformatted column name to a formatted column name.
       *
       *  That is, insert a space when a new uppercase character is found,
       *  insert multiple upper case characters are grouped together.
       *
       *  @param columnName  unformatted column name
       *  @return the formatted column name
       */
      public static String formatColumnName(String columnName) {
            if (columnName.length() < 3) return columnName;

            StringBuffer buffer = new StringBuffer( columnName );
            boolean isPreviousLowerCase = false;

            for (int i = 1; i < buffer.length(); i++) {
                  boolean isCurrentUpperCase = Character.isUpperCase( buffer.charAt(i) );
                  if (isCurrentUpperCase && isPreviousLowerCase){
                        buffer.insert(i, " ");
                        i++;
                  }
                  isPreviousLowerCase = ! isCurrentUpperCase;
            }
            return buffer.toString();
      }
}



No comments:

Post a Comment