20

Can anybody please mention the best available opensource odbc:jdbc driver to read / write dbf.? I have a dbf file which I would like to query (select/update) via a web application (Tomcat app).

Any help/tips would be appreciative.

Thank you.

pinkb
  • 543
  • 2
  • 6
  • 15

3 Answers3

16
try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String connString="jdbc:odbc:Driver={Microsoft dBASE Driver (*.dbf)};DefaultDir=E:\\db";//DeafultDir indicates the location of the db
            Connection connection=DriverManager.getConnection(connString);
            String sql="SELECT * FROM table_name where condition";// usual sql query
            Statement stmt=connection.createStatement();
            ResultSet resultSet=stmt.executeQuery(sql);
            while(resultSet.next())
            {
                System.out.println();
            }
            System.out.println();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

It works. And I guess there will be no need to explore for other (open/closed) apis as Java has provided an excellent way to read/write dbf.

Thank you all.

pinkb
  • 543
  • 2
  • 6
  • 15
  • 1
    can you tell how with this driver to disable autocommit? Because using `connection.setAutoCommit(false)` method throws `java.sql.SQLException: [Microsoft][ODBC dBase Driver]Optional feature not implemented` – Boro Feb 10 '12 at 11:53
  • thanks, worked for me even on win7-64bit – coding_idiot Jun 11 '12 at 07:43
  • 1
    @XCoder Could you say if there were some special configuration steps involved in getting it to run on 64bit machine? I am having a issue here that when I use the driver with Win7 64bit it simply returns the SQLException with the following message: `[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified`, when all worked on my old 32bit machine? – Boro Dec 17 '12 at 16:24
  • @Boro DSN setting is different in win64, but it has nothing to do with java code. The DSN that you set up ain't the 64-bit one, this link might help further : http://support.microsoft.com/kb/942976 – coding_idiot Dec 29 '12 at 13:44
  • @XCoder thanks for the link. It was very helpful. I also found an easier to read explanation over here on SO http://stackoverflow.com/a/12537390/613495 I basically set the User DSN and connect using it rather than the driver directly. Thanks again for the info. – Boro Jan 07 '13 at 10:43
  • This didn't work for me. I was trying to acces a Visual FoxPro database. I managed to do it and posted [how to do it](http://stackoverflow.com/a/21945208/2796922) – elysch Feb 21 '14 at 21:13
  • 4
    The JDBC ODBC Bridge is now deprecated and will be gone in JDK 8. – Alan B Oct 27 '16 at 09:58
  • Drawback: this code is limited to Windows and not portable to other platforms. OK for a quick hack, or something that already has Windows dependencies—for other purposes, one of the libraries suggested in the other answers is probably the better choice. – user149408 Mar 21 '21 at 12:41
13

You can try to use https://github.com/iryndin/jdbf - a simple Java library to read/write DBF files. I am the author of this library and it works quite good in my production apps. It is very simple.

Please give it a try.

iryndin
  • 530
  • 1
  • 5
  • 11
3

dans-dbf is a good option to access dbf files, but it has a custom api (ie: not sql).

I would recommend you to dump the dbf files into db tables (mysql with myisam engine will do the trick or innodb if transaction/consistency checking is required).

Then you can dump back to dbf as needed.

William
  • 3,511
  • 27
  • 35
  • Depending on the size of the data on hand, I'd favor an in-memory Java database such as hsqldb or h2 and write a little wrapper using dans-dbf. – VH-NZZ Dec 10 '14 at 18:03