0
votes

I have a spring-batch file that extracts data from a database and writes it to a .CSV file.

I would like to add the names of the columns that are extracted as the headers of the file without hard coding them on the file.

Is possible to write the header when I get the results or is there another solution?

Thanks

1
Can you post some of the code you have thus far? Also what does this have to do with java? - CalebB
Because i use java to access the database. I will post some code - esfomeado12

1 Answers

0
votes
fileItemWriter.setHeaderCallback(new FlatFileHeaderCallback() {
            public void writeHeader(Writer writer) throws IOException {
                writer.write(Arrays.toString(names));
            }
        });

[names] can be fetched using reflections from the domain class you created for the column names to be used by rowMapper, something like below :

private String[] reflectFields() throws ClassNotFoundException { 
        Class job = Class.forName("DomainClassName");
        Field[] fields = FieldUtils.getAllFields(job);
        names = new String[fields.length];
        for(int i=0; i<fields.length; i++){
            names[i] = fields[i].getName(); 
        }
        return names;
    }