开发者

How can I query a DBF file in java

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?


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

}


There is a registry of JDBC drivers at http://developers.sun.com/product/jdbc/drivers . You can select your platform, for example dBase for DBF files and look for a driver. For dBase it lists a bunch of drivers and many of them are type 4. I didn't look into details whether any of these drivers are free.


You might also try using Jython along with my python dbf module. It allows for searching (brute force and indexed) as well as pythonic usage patterns. Note: I haven't tested with Jython, but am very responsive to help requests.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜