The CsvJdbc Logo

CsvJdbc - a JDBC driver for CSV files

About

CsvJdbc is a simple read-only JDBC driver that uses Comma Separated Value (CSV) files as database tables. It is ideal for writing data importing programs.

How is it used

The CsvJDBC driver is used just like any other JDBC driver. The csvjdbc.jar file should be included in your application's classpath.

The driver class is org.relique.jdbc.csv.CsvDriver The connection URL is jdbc:relique:csv:csvdir, where csvdir is the directory in which the .csv files are found.

This example code shows how the driver is used.


import java.sql.*;

public class DemoDriver
{
  public static void main(String[] args)
  {
    try
    {
      // load the driver into memory
      Class.forName("org.relique.jdbc.csv.CsvDriver");

      // create a connection. The first command line parameter is assumed to
      //  be the directory in which the .csv files are held
      Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + args[0] );

      // create a Statement object to execute the query with
      Statement stmt = conn.createStatement();

      // Select the ID and NAME columns from sample.csv
      ResultSet results = stmt.executeQuery("SELECT ID,NAME FROM sample");

      // dump out the results
      while (results.next())
      {
        System.out.println("ID= " + results.getString("ID") + "   NAME= " + results.getString("NAME"));
      }

      // clean up
      results.close();
      stmt.close();
      conn.close();
    }
    catch(Exception e)
    {
      System.out.println("Oops-> " + e);
    }
  }
}

Advanced Options

The driver also supports a number of parameters that change the default behaviour of the driver.

These properties are:

separator
Used to specify a different column separator (Default is ',').
suppressHeaders
Used to specify if the first line contains column header information (Default is false; column headers are on first line).
fileExtension
Used to specify a different file extension (Default is ".csv")
charset
Used to specify a different than default charset encoding of input file (default is same VM default charset)
raiseUnsupportedOperationException
Used to specify whether an UnsupportedOperationException must be raised when calling a connection method (such as setAutoCommit()) having no sense with csvJdbc (Default is "false")

This following example code shows how these properties are used.

  ...

  Properties props = new java.util.Properties();

  props.put("separator","|");              // separator is a bar
  props.put("suppressHeaders","true");     // first line contains data
  props.put("fileExtension",".txt");       // file extension is .txt
  props.put("charset","ISO-8859-2");       // file encoding is "ISO-8859-2"
  props.put("raiseUnsupportedOperationException","false");       // don't raise unsupported operation exception (when not relevant)

  Connection conn = Drivermanager.getConnection("jdbc:relique:csv:" + args[0],props)

  ...
         

Scrollable ResultSets

The driver also now supports scrollable result sets.

This following example code shows how these are used.

  ...

  Connection conn = Drivermanager.getConnection("jdbc:relique:csv:" + args[0],props)

  Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 0);

  ResultSet results = stmt.executeQuery("SELECT ID,NAME FROM sample");

  resulst.next();
  ...
  results.first();
  ...
  results.previous();
  ...
  results.relative(2);
  ...
  results.absolute(2);
  ...
         

FAQ

Does the driver support UPDATE and DELETE statements ?
No the driver does not. It is a read-only driver and as such only supports the SELECT statement.

Does the driver support WHERE and ORDER BY clauses ?
The driver does not support the ORDER BY clause. It does support the WHERE clause but only for a single field and only for the equals function. The field must be part of the SELECT statement.

More Information

The latest version of the software can be found at http://www.sourceforge.net/projects/csvjdbc.

Last modified: $Id: index.html,v 1.1 2001/01/22 10:17:30 jackerm Exp $