I would like to write the results of my JDBC query as csv file, including a line with the names of the columns.
I am able to write results by defining a JDBC PreProcessor in which I assign the Result variable name
to resultSet
, and a JSR223 PreProcessor where I include the below script to write to csv.
However, the variable returned by vars.getObject("resultSet");
is a Collection
, so I am unable to get the column name information. Is it possible to pass the ResultSet
object to the JSR223 PreProcessor so that I can export column names?
resultSet = vars.getObject("resultSet");
result = new StringBuilder();
//for (int i = 0; i <= resultSet.getMetaData().getColumnCount(); i++) {
// result.append(resultSet.getMetaData().getColumnName(i));
// result.append(',');
//}
//result.append(System.getProperty("line.separator"));
for (Object row : resultSet ) {
iter = row.entrySet().iterator();
while (iter.hasNext()) {
pair = iter.next();
result.append(pair.getValue());
result.append(",");
}
result.append(System.getProperty("line.separator"));
}
org.apache.commons.io.FileUtils.writeStringToFile(new File("/tmp/data", "results.csv"), result.toString(), "UTF-8");