It's clean, the Java method without any external API. All comments are included.
public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } in UTF8 public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } f)) { public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } new OutputStreamWriter (os, "UTF- public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } be skipped public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } is reached public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } ) { public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } names public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } will be written public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } } count public void rsToCSV(ResultSet rs, File f, boolean columnNames) throws SQLException, FileNotFoundException, IOException{ //get metadata from ResultSet ResultSetMetaData metaData = rs.getMetaData(); //get number of columns from metadata int columnCount = metaData.getColumnCount(); //write 3 bytes to output stream to encode csv file in UTF8 try (OutputStream os = new FileOutputStream(f)) { os.write(239); os.write(187); os.write(191); //initialize PrintWriter class try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"))) { //if columnNames is true column names will be written in csv file //if columnNames is false column names will be skipped if(columnNames){ //this will loop until number of columns is reached for(int i = 1; i<=columnCount; i++){ //this writes column name to csv file pw.print(metaData.getColumnName(i)); //if i is less then column count //comma will be written to separate column names if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } //while result set holds rows while (rs.next()){ //this will loop until column count is reached for (int i = 1; i <=columnCount; i++){ //data from column of index i is written to csv pw.print(rs.getObject(i)); //if columns index is less then column count //comma will be written to separate data if(i<columnCount){ pw.print(","); pw.flush(); } //if column count is reached //new line will be written if(i==columnCount){ pw.println(); pw.flush(); } } } } } }