How can I query a DBF file in java

user646539 picture user646539 · Aug 4, 2011 · Viewed 18.3k times · Source

I have a rather large DBF file, about 40 megs, that I need to be able to query. Right now I am reading the DBF, it's just tabular text, into a h2 database and querying the h2 database. This works, but seems... stupid. I've been looking for a type 4 JDBC driver for DBF, but haven't had any luck. What is the proper way to do this?

Answer

galisha picture galisha · Dec 25, 2012

For such tasks as data exchange for large size - this way for using JDBC bridge seems to me very slowly. Also you can get some runtime errors during data exchange.

The best way is using file IO libraries. After some years I issued such pure pure lightweight library and will present you. (Under LGPL)

You may download it from here

See dbf reading code below. It is very simple.

public class Fp26Reader {
  private static void testRead() {
        DbfIterator dbfIterator = DbfEngine.getReader(

        Fp26Reader.class.getResourceAsStream("FP_26_SAMPLE.DBF"), null);

        while (dbfIterator.hasMoreRecords()) {
              DbfRecord dbfRecord = dbfIterator.nextRecord();
              String string = dbfRecord.getString("string");
              float sumFloat = dbfRecord.getFloat("sum_f");
              BigDecimal sumNumeric = dbfRecord.getBigDecimal("sum_n");
              boolean bool = dbfRecord.getBoolean("bool_val");
              Date date = dbfRecord.getDate("date_val");

              System.out.println(string + " " + sumFloat + " " + sumNumeric + " "+ bool + " " + date);
        }
  }

  public static void main(String[] args) {
        Fp26Reader.testRead();
  }

}