SUM
SUM

Reputation: 1661

Write more than one database results to csv file

I am writing the result set of 1st database query result set to first 2 columns of csv file.

Then I am trying to write header name 'HSS1' in third column of csv which is going to have result set of another database table. Am not doing the second step the right way.

1st step:

        fw = new FileWriter(filename);
        fw.append("CID");
        fw.append(',');
        fw.append("Uniques");
        fw.append('\n');

        rs=stmt.executeQuery(sql);

            while(rs.next()){

                fw.append(rs.getString(1));
                fw.append(',');
                fw.append(rs.getString(2));
                fw.append('\n');
                fw.flush();

Writing header name in csv file for the second database query

second step

try{

    fw.append(",");
    fw.append(",");
    fw.append("HSS1");
    fw.append('\n');

    fw.close();

HSS1 gets written to 3rd column but it's not the header.

Thanks

Upvotes: 0

Views: 381

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328850

append() always appends to the end of the file.

What you need to do is move the code to the right place and iterate over both database queries at the same time (in one loop).

Upvotes: 1

Related Questions