Posted by Somesh Shinde
On
Wednesday, 17 August 2016
Save SqlQuery result to FlatFiles
public void printToFile( ResultSet rs, String path ) throws IOException {
PrintStream out = new FileOutputStream( path );
int cols = rs.getMetaData().getColumnCount();
while( rs.next() ) {
for( int i = 0; i < cols ; i++ ) {
out.printf("%s,", rs.getObject( i ) );
}
out.println();
}
// add exception handling and such...
// or move the from here.
out.close();
rs.close();
}
And then call it like this:
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
printToFile( rs, "/some/file" );