0

I'm very new to Java and am looking for some assistance. I have a resultset that I'm able to either print (without titles, but that's for a different time), or can produce a jtable.

Ultimately, though, I want to be able to send it to excel. I don't really care whether it is a CSV or XLS, I just want to be able to get it to Excel.

I downloaded OpenCSV and have it added to the build path, but I still can't get it to work. I've even tried forklifting multiple different version of code, but nothing still works. I have the code below.

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JTable;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.io.*;


public class RuneDW {


    public static void main(String[] args) throws SQLException, IOException {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        JTable table = null;
        File file = File.createTempFile("test",".csv");

        try {

            conn = DBUtil.getConnected(DBType.EDW);
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery("select trunc(acct_d) as acct_d, cust_ord_i, sls_q from fin_sls where rownum<=13" +
                    "and acct_d>= sysdate-2");
            ArrayList<String> arrayList = new ArrayList<String>();
            ResultSetMetaData metadata = rs.getMetaData();
            int numberOfColumns = metadata.getColumnCount();
            while (rs.next()) {              
                int i = 1;
                while(i <= numberOfColumns) {
                    arrayList.add(rs.getString(i++));
                }
                System.out.println(rs.getString("acct_d")+rs.getString("cust_ord_i")+rs.getString("sls_q"));

        }
        } finally {
        }

        }
    }
  • You create a file object, but then never use it. Try using a `FileWriter` and/or a `BufferedWriter` (see http://stackoverflow.com/questions/14503595/write-file-using-bufferedwriter-in-java) – Gus Jun 13 '14 at 20:53
  • Just so you know, a CSV is nothing more than a comma delimited file. each row is a single line in the file. So for example you can have the following:Product,Price,Intel CPU,100$. Again You will have to add a newline(\n) for each line – Rami Del Toro Jun 13 '14 at 20:56

0 Answers0