Exporting data from database to CSV files is a common task of any software applications. In this article, I will guide you how to write Java code to read data from a database and write to a CSV file.

The technique is simple. We use JDBC to read data from database and use File I/O to write CSV file. And a JDBC driver library for the underlying database is necessary (MySQL is used in this post).

Suppose that we have a table with the following structure:

table review structure

And this table contains some data like this:

data-in-table

First, you will learn how to write Java code to export data from this table to a CSV file. And then I will share with you how to write general, reusable code that works with any table.

 

1. Simple Java code example to export from database to CSV file

The following code is for a simple Java program that connects to a MySQL database reads all rows from the review table and write that data to a CSV file:

package net.codejava;

import java.io.*;
import java.sql.*;

/**
 * A simple Java program that exports data from database to CSV file.
 * @author Nam Ha Minh
 * (C) Copyright codejava.net
 */
public class SimpleDb2CsvExporter {

	public static void main(String[] args) {
	    String jdbcURL = "jdbc:mysql://localhost:3306/sales";
	    String username = "root";
	    String password = "password";
	    
	    String csvFilePath = "Reviews-export.csv";
	    
	    try (Connection connection = DriverManager.getConnection(jdbcURL, username, password)) {
	    	String sql = "SELECT * FROM review";
	    	
	    	Statement statement = connection.createStatement();
	    	
	    	ResultSet result = statement.executeQuery(sql);
	    	
	    	BufferedWriter fileWriter = new BufferedWriter(new FileWriter(csvFilePath));
	    	
	    	// write header line containing column names    	
	    	fileWriter.write("course_name,student_name,timestamp,rating,comment");
	    	
	    	while (result.next()) {
	    		String courseName = result.getString("course_name");
	    		String studentName = result.getString("student_name");
	    		float rating = result.getFloat("rating");
	    		Timestamp timestamp = result.getTimestamp("timestamp");
	    		String comment = result.getString("comment");
	    		
	    		if (comment == null) {
	    			comment = "";	// write empty value for null
	    		} else {
	    			comment = "\"" + comment + "\""; // escape double quotes
	    		}
	    		
	    		String line = String.format("\"%s\",%s,%.1f,%s,%s", 
	    				courseName, studentName, rating, timestamp, comment);
	    		
	    		fileWriter.newLine();
	    		fileWriter.write(line);	    		
	    	}
	    	
	    	statement.close();
	    	fileWriter.close();
			
		} catch (SQLException e) {
			System.out.println("Datababse error:");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("File IO error:");
			e.printStackTrace();
		}
	    
	}

}


As you can see in this program, it is written for a specific table whose column names are known. Run this program and you would see the Reviews-export.csv file is generated with the following content:

simple-export-result

Note that the values of the ID field are not exported. The values of some columns are put inside double quotes so it is still valid if the text contains commas.

 

2. Advanced Java code example to export from database to CSV file

Let’s see how to code a more generic program that can work with any tables. Following is code of the full program:

package net.codejava;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;

/**
 * An advanced Java program that exports data from any table to CSV file.
 * @author Nam Ha Minh
 * (C) Copyright codejava.net
 */
public class AdvancedDb2CsvExporter {
	private BufferedWriter fileWriter;
	
	public void export(String table) {
	    String jdbcURL = "jdbc:mysql://localhost:3306/sales";
	    String username = "root";
	    String password = "password";
	    
	    String csvFileName = getFileName(table.concat("_Export"));
	    
	    try (Connection connection = DriverManager.getConnection(jdbcURL, username, password)) {
	    	String sql = "SELECT * FROM ".concat(table);
	    	
	    	Statement statement = connection.createStatement();
	    	
	    	ResultSet result = statement.executeQuery(sql);
	    	
	    	fileWriter = new BufferedWriter(new FileWriter(csvFileName));
	    	
	    	int columnCount = writeHeaderLine(result);
	    	
	    	while (result.next()) {
	    		String line = "";
	    		
	    		for (int i = 2; i <= columnCount; i++) {
	    			Object valueObject = result.getObject(i);
	    			String valueString = "";
	    			
	    			if (valueObject != null) valueString = valueObject.toString();
	    			
	    			if (valueObject instanceof String) {
	    				valueString = "\"" + escapeDoubleQuotes(valueString) + "\"";
	    			}
	    			
	    			line = line.concat(valueString);
	    			
	    			if (i != columnCount) {
	    				line = line.concat(",");
	    			}
	    		}
	    		
	    		fileWriter.newLine();
	    		fileWriter.write(line);	    		
	    	}
	    	
	    	statement.close();
	    	fileWriter.close();
			
		} catch (SQLException e) {
			System.out.println("Datababse error:");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("File IO error:");
			e.printStackTrace();
		}
	    
	}

	private String getFileName(String baseName) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
		String dateTimeInfo = dateFormat.format(new Date());
		return baseName.concat(String.format("_%s.csv", dateTimeInfo));
	}
	
	private int writeHeaderLine(ResultSet result) throws SQLException, IOException {
    	// write header line containing column names
    	ResultSetMetaData metaData = result.getMetaData();
    	int numberOfColumns = metaData.getColumnCount();
    	String headerLine = "";
    	
    	// exclude the first column which is the ID field
    	for (int i = 2; i <= numberOfColumns; i++) {
    		String columnName = metaData.getColumnName(i);
    		headerLine = headerLine.concat(columnName).concat(",");
    	}
    	
    	fileWriter.write(headerLine.substring(0, headerLine.length() - 1));
    	
    	return numberOfColumns;
	}
	
	private String escapeDoubleQuotes(String value) {
		return value.replaceAll("\"", "\"\"");
	}
	
	public static void main(String[] args) {
		AdvancedDb2CsvExporter exporter = new AdvancedDb2CsvExporter();
		exporter.export("review");
		exporter.export("product");
	}	
}
You pass a table name to the export() method and it does all the heavy work. The CSV file name is generated based on the table name, followed by _Export and the current date time:

String csvFileName = getFileName(table.concat("_Export"));
The method getFileName() is written as follows:

private String getFileName(String baseName) {
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
	String dateTimeInfo = dateFormat.format(new Date());
	return baseName.concat(String.format("_%s.csv", dateTimeInfo));
}
So if the table name is Review, then the CSV file name would be Review_Export_2019-10-14_20-52-55.csv. Adding timestamp to the CSV file name would be useful for the end users who will be able to differentiate each data export.

The writeHeaderLine() method writes the column names in first line of the CSV file. It uses ResultSetMetaData to get the names of the columns – that means it can work with any tables.

Note that the program doesn’t export values of the ID column (it is supposed to be always the first column).

Then you can use this program to export data from any tables you wish, like this:

AdvancedDb2CsvExporter exporter = new AdvancedDb2CsvExporter();

exporter.export("review");
exporter.export("product");
That’s Java code example to export data from database to CSV file. I hope you find this article helpful and use the code as a reference to export your down data.

 

Other Java Coding Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#5Zeba Sheikh2024-03-27 06:37
Hi , this program works but I am not able to find where the csv file is getting generated. Could you please help me for that. Thanking you in anticipation
Quote
#4Avinash Kumar Paswan2021-05-25 05:18
Hello,
I am using Oracle database and I want to export tables into (.sql) file formate using command line. Please help me if someone have any idea about it.
Quote
#3Rangga2020-12-16 23:43
I have try this, but the output still zero, why?
Quote
#2Rangga2020-12-16 22:30
This is cool, thanks sir
Quote
#1Jagan2020-04-11 07:42
getting below error
ava.math.BigDecimal cannot be cast to java.lang.String ......Print not filled.

The variable I'm using is pulled from an Oracle database and is a BigDecimal data type. In my Value Expression, I'm setting the Value Class Name as java.math.BigDecimal as well. I've tried passing the variable as a string and that doesn't work either.

Please help.
Quote