A few issues with this code. The reason you're only getting the last result is because of this line:
fWriter = new FileWriter("Exports/Transfers/" + /* frmNm.replace(":", "_") */"EBW_" + GtDates.fdate + ".csv", false);
This line is inside your loop. The false
as the last parameter tells FileWriter
not to append. In other words, a false
means overwrite the file if it exists. Since this is in your loop, each result overwrites the file that the last result created. You should create the FileWriter
outside of your loop, probably in a try with resources. That will allow you to remove your writer.close()
call, which should have been in a finally
block anyway.
Not related to your original question but something you should be aware of: You're creating a new Statement
with each loop iteration. This can be an expensive operation. You should use a PreparedStatement
instead. Create it outside your loop and then just set the parameter and execute it inside the loop. It also implements AutoCloseable
, so you can create it in a try with resources too, probably the same one you create your FileWriter
in.