I am trying to export a lot of large tables from a MS Access db with java using the jdbc:odbc b开发者_开发问答ridge. 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.
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();
}
You could use opencsv http://opencsv.sourceforge.net
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>
精彩评论