Skip to content

Instantly share code, notes, and snippets.

@sdaza
Last active November 1, 2020 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdaza/2c55044c9dbfcc38c8e5d8ded0a84a17 to your computer and use it in GitHub Desktop.
Save sdaza/2c55044c9dbfcc38c8e5d8ded0a84a17 to your computer and use it in GitHub Desktop.
From SQL to CSV
public void sqlToCSV (ResultSet rs, String filename) {
try {
FileWriter fw = new FileWriter(filename + ".csv");
int cols = rs.getMetaData().getColumnCount();
for(int i = 1; i <= cols; i ++){
fw.append(rs.getMetaData().getColumnLabel(i));
if(i < cols) fw.append(',');
else fw.append('\n');
}
while (rs.next()) {
for(int i = 1; i <= cols; i ++){
fw.append(rs.getString(i));
if(i < cols) fw.append(',');
}
fw.append('\n');
}
fw.flush();
fw.close();
} catch (Exception e) {
getEngine().pause();
traceln("--> An Exception happened during initialization, continue? ...");
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment