The CsvJdbc Logo

CsvJdbc - a JDBC driver for CSV files

About

CsvJdbc is a simple read and write JDBC driver that uses Comma Separated Value (CSV) files as database tables.It can be used for backup of your database, for store initial data for applications ...

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();

      // CsvDriver also support PreparedStatements.
      // PreparedStatement.setString() and PreparedStatement.setBytes() are methods that can be used.
      // PreparedStatement.getString() and PreparedStatement.getBytes() are methods that can be used.
      //
      // setBytes and getBytes are used to work with binary objects
      //
      // create PreparedStatement
      PreparedStatement pstmt = conn.prepareStatement("INSERT INTO sample ( ID, NAME ) VALUES (?, ?) ");
      pstmt.setString(1, "1");
      pstmt.setString(2, "somename" );
      pstmt.executeUpdate();
      pstmt.clearParameters();
      pstmt.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)
create
If true, driver will create directory(s) for database.Default is false.
maxFileSize
This parameter is used to constraint max size of files. For example, when you inserting data into table, if file size become bigger then specified with this param, driver will create new file for same table, and add extension .csvextension. Example : if table name is TABLE.csv, new files will be named TABLE001.csvextension, TABLE002.csvextension ... . Default is 1500000000. Value is in bytes
lineBreakEscape
This parameter is used when in data there are line breaks. Driver will replace all line breaks with special string. Default value is : ~CSVLB~
carriageReturnEscape
This parameter is used when in data there are carriage return. Driver will replace all carriage returns with special string. Default value is : ~CSVCR~

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("maxFileSize",10000);            // max size of files in bytes.
  props.put("create","true");                // driver will create directory(s)
  props.put("lineBreakEscape","ELB");        // all line breaks will be replaces with ELB
  props.put("carriageReturnEscape","ECR");   // all carriage return will be replaces with ECR

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

  ...

  Also, this parameters can be passed when creating connection on the other way :

  Connection conn = Drivermanager.getConnection("jdbc:relique:csv:databasename;separator=@;maxFileSize=10000;create=true")

  If you want to have double quotes in data ( " ), then driver will represent double quote in data with double occurence
  of double quotes, like this : "".
  Null values are represent in file with nothing between SEPARATOR( comma or something else ) -e.g :  ,, means ,NULL VALUE,.
  When inserting null value into csv, do not put it between quotes like this 'null', because this mean for driver that
  this is string "null".
  Example :
  INSERT INTO TABLE ( nullcolumn ) VALUES ( null );