Last active
November 1, 2020 15:28
-
-
Save sdaza/2c55044c9dbfcc38c8e5d8ded0a84a17 to your computer and use it in GitHub Desktop.
From SQL to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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