如何使用java查询DBF文件



我有一个相当大的DBF文件,大约40 mb,我需要能够查询。现在我正在阅读DBF,它只是表格文本,进入h2数据库和查询h2数据库。这是有效的,但似乎……愚蠢的。我一直在为DBF寻找type 4 JDBC驱动程序,但没有找到任何运气。正确的做法是什么?

在http://developers.sun.com/product/jdbc/drivers上有一个JDBC驱动程序注册表。您可以选择您的平台,例如DBF文件的dBase,并查找驱动程序。对于dBase,它列出了一堆驱动程序,其中许多是type 4。

对于像数据交换这样的大容量任务——这种使用JDBC桥的方式在我看来很慢。此外,在数据交换过程中可能会出现一些运行时错误。

最好的方法是使用文件IO库。若干年后我发布了这样的纯纯轻量级库并将其呈现给大家。(LGPL)

你可以从这里下载

请参阅下面的dbf读取代码。这很简单。

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

}

您也可以尝试使用Jython和我的python dbf模块。它允许搜索(蛮力和索引)以及python使用模式。注意:我还没有测试过Jython,但是我对帮助请求的响应非常快。

最新更新