There is no newline character being written after the column names have been written, resulting in the first row of data appearing as column headers. Write a newline character after the for
loop that writes the column names.
ResultSetMetaData to Filewriter/CSV issue JAVA
-
10-10-2022 - |
Domanda
This code prints 4 data headers and data below in my CSV file correctly in columns A-D However, in columns E-H there is a Random set of data.
ResultSetMetaData rsmd = result.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i < columnCount + 1; i++) {
String name = rsmd.getColumnName(i);
out.print(name);
if (i != columnCount + 1) {
out.print(",");
System.out.print(i);
}
}
while (result.next()) {
out.println(String.format("%s,%s,%s,%s", result.getString(1),
result.getString(2), result.getString(3),
result.getString(4)));
}
result.close();
statement.close();
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow