Davey
Davey

Reputation: 101

How to export and save tables to .csv from access database in java

I am trying to export a lot of large tables from a MS Access db with java using the jdbc:odbc bridge. I wanted to save these tables to a CSV file first was wondering what would the best way to do this would be? any help would be appreciated.

Upvotes: 1

Views: 2505

Answers (3)

John
John

Reputation: 4006

A complete example is here:

https://github.com/NACHC-CAD/access-to-csv-tool

This code includes complete examples in the test code and does a complete export of the northwind data base.

See the class AccessToCsvUtil for the code to convert a single table to a .csv file.

See the class WriteToCsvIntegration test to see code that exports all of the tables in the database.

This utility is based on the ucanaccess jdbc tool and the Apache commons-csv tool.

    <dependency>
        <groupId>net.sf.ucanaccess</groupId>
        <artifactId>ucanaccess</artifactId>
        <version>4.0.4</version>
    </dependency>


    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.8</version>
    </dependency>

Upvotes: 0

Steve
Steve

Reputation: 363

You could use opencsv http://opencsv.sourceforge.net

Upvotes: 1

Andreas
Andreas

Reputation: 737

Fetch the values and write a standard text file line by line separating the values. I#m sure there are some libs for this purpose

try
{
 FileWriter writer = new FileWriter("c:\\temp\\MyFile.csv");
 while(result.next())
 {
 for(int i = 0; i < columnSize; i++)
 {
    writer.append(result.getObject(i));
    if(i < columnSize - 1)
       writer.append(',');
 }
 writer.append('\n');
 }
 }
 catch(Exception e)
{
  e.printStackTrace();
}

Upvotes: 3

Related Questions