l0r3nz4cc10
l0r3nz4cc10

Reputation: 1283

Writing SQL query result to csv fail : uncomplete line

I am trying to write some query results in a csv file.

I thought it was working fine, until I saw the last line of the file after it's been written:

value;nettingNodeData;BLE57385;CVAEngine;BLE;;.4;;BDR;NA;ICE;;RDC;;CVAEngine;;4841263;RDC value;ne

The part where I am writing the file :

public int getLeNodes(String runId, File file) {
    Connection connect = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int lines = 0;

    try {
        connect = newConnection();
        ps = connect.prepareStatement(HIER_NTT.replace("?", runId));
        ps.setFetchSize(1500);
        rs = ps.executeQuery();

        lines += nnpw.writeCore(file, rs);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        close(rs, ps, connect);
    }
    return lines;
}

public int writeCore(File file, ResultSet rs) throws FileNotFoundException, IOException {
    int count = 0;
    try {
        BufferedWriter output = new BufferedWriter(new FileWriter(file, true));
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs.next()) {
            for (int col = 1; col <= colCount; col++) {
                try {
                    String val = rs.getString(col);
                    if (rs.wasNull()) {
                        val = "";
                    }
                    output.append(val);
                } catch (ArrayIndexOutOfBoundsException e) {
                    String dec = rs.getBigDecimal(col).toPlainString();
                    System.err.println(String.format("%s %d %s %s %d %s %d", rs.getString(1), col,
                            rsmd.getColumnTypeName(col), file, count, dec, dec.length()));
                    output.append(dec);
                }
                if (col < colCount) {
                    output.append(";");
                }
            }
            output.newLine();
            count++;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return count;
}

And the SQL (HIER_NTT):

select 'value', 'nettingNodeData', 'BLE' || d.deal_numadm, 
'CVAEngine', 'BLE', '', (1-ntt.lgd_cp), '', 'BDR', 'NA', 'ICE', '', 'RDC', '',
'CVAEngine', '', d.ntt_id, 'RDC' from ntt ntt 
join deals d on d.ntt_id = ntt.ntt_id and d.deal_scope='Y' 
join dt_runs r on r.run_id = ntt.run_id 
where r.run_id=? and d.deal_numadm!=0 group by d.deal_numadm, d.deal_nummas, d.ntt_id, ntt.lgd_cp

So, why does my file end abruptly like this?

Upvotes: 0

Views: 56

Answers (1)

tim_yates
tim_yates

Reputation: 171184

You should call output.close() when you are done writing to the file. The missing output is probably still in the buffer when the java process exits

Upvotes: 1

Related Questions