0

I'm trying to change an sql query result into csv format then send that to an API for processing. When I use CSVPrinter (org.apache.commons.csv.CSVPrinter) it always adds: "org.apache.commons.csv.CSVPrinter@5ec70441" to the output.

I would like to know if it's because of the way I'm using it, or is it a standard thing that I need to parse out first?

This is the code, I tried outputting the CSV a couple ways but get the same thing every time.

public void ScoreTable(String tableName) throws SQLException, IOException {
    DataB db = new DataB();
    ResultSet rs;
    rs = db.executeQuery("SELECT NtNo, SchedDate, SchedTime, Duration, WC, Sequence, CBy FROM " + tableName + " ");
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
    CSVPrinter csvPrinter;
    csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(rs));
    csvPrinter.printRecords(rs);
    String scoreCSV = csvPrinter.toString();
    System.out.println("String contains: " + scoreCSV);
    csvPrinter.close();
    populateScore pa = new populateScore();
    String theScore = pa.getCsvScore("21", scoreCSV);
    System.out.println("API outputs : " + theScore);
}
Mikhail2048
  • 1,715
  • 1
  • 9
  • 26
MIchael
  • 3
  • 2
  • 1
    See https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – tgdavies Mar 19 '23 at 03:06

1 Answers1

2

Your CSVPrinter is writing to the stdout of the JVM process. When all data is written, you add new line to stdout:

System.out.println("String contains: " + scoreCSV);

As far as I managed to understand your code, you are trying to get the result from the CSVPrinter by calling its toString method. That method has nothing to do with CSV, it is a method defined in java.lang.Object. CSVPrinter does not override this method. So of course you get the default Object#toString() implementation added to the output.

Again, I just guessing, maybe writing the result to the JVM stdout is not what you want, but if it is, then just do not print the result of CSVPrinter#toString() method.

Mikhail2048
  • 1,715
  • 1
  • 9
  • 26