B4J Question Aceesing DBF files

JTmartins

Active Member
Licensed User
Longtime User
Hi,

Does anyone knows any form to access DBF files trough B4J ?

I can't find any proper free JDBC driver, and I don't feel the courage to read DBF specifications and do a kind of driver myself.

If some one has a solution and a litle example of how to connect to a DBF (visual Fox Pro) with B4J, I would appreciate.

Many thanks
 

charlesg

Member
Licensed User
Longtime User
Hi,

Very interesting. Pity jdbf does not support indexing.

Would it be possible to give us a simple B4J example using jdbf and Javaobject? Would be much appreciated.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
In my case I only need to read one field from the DBF. I do not need to write, or mess around much with it. I wll post a sample as soon as I get home. No prob.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Here we go, as you can see, to read (and in my particular case is extremly easy)

First you add this line to the Project Atributes

B4X:
#AdditionalJar: jdbf-1.2.jar

Then I have a small sub, that fetches the only field I need. I' can´t send you the dbf, as is from the company I work for...But I'm sure you will get the idea.

B4X:
Sub BuscaAlvara
    novaBD.InitializeNewInstance ("com.hexiong.jdbf.DBFReader", Array As Object ("here you place the path to the file\file.dbf"))
    Private dados() As Object
    dados=novaBD.RunMethod("nextRecord",Null)
    alvara=dados(13)
   novaBD.RunMethod ("close",Null) ' this closes the dbf file
End Sub

In my case I needed the field Nº 13. and I store it in alvara wich is a global string variable

If you read the docs from JDBF you can use the method "HasNextRecord" (wich returns True or False) to ensure there are more records, and then act accordingly to iteract trough records. I do not need it, as the field I was looking for I know it's always there and my dbf only had one record . Only it's value changes from department to department of the company.

If you run in debug mode you will see all your fields in the array dados.

Hope it helps.

JM
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Very Good :).

Triggered me to build (simple) B4J HowTo example projects. Download here.
  • Project 1 uses external library jDBF
  • Project 2 uses xBaseJ. xBaseJ offers more functionality.
  • Project 3 uses jDBC (driver is not for free, download the driver)
Pls note the extensive use of JavaObjects.

EDIT 2014.11.22: Added Project 3 jdbc:dbf driver example
 
Last edited:
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Hi rwblin,

First I must thank you for your samples. They are much better than the example I gave above, wich was very simple.

I've played before with XBaseJ, and did not manage to make it work with my dbf's, wich are from VisualFoxPro 7.

Tested just now your app and got the same error while running, wich is

java.lang.reflect.InvocationTargetException
...

Caused by: org.xBaseJ.xBaseJException: Wrong Version 48


Not sure, but I presume XbaseJ does not read VFP dbf's.

The same hapened with some files and Jdbf, but not with all of them and it worked with the one I needed. So regarding VFP, it works, but it depends on the dbf's.

UPDATE - Actually I find out that the problem with JDBF is only related with DBf's with memo and it apears to open all the others.

However still no success with XbaseJ and VFP dbf's and after reading posts in XbaseJ Discussion, indeed it is not compatible with VFP files.
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

thanks for reply.

Another way to access DBF files is using a jdbc:dbf driver. I have added another example (updated the download in post #7).
It shows how to access (read/delete/insert) a MS Visual FoxPro database file, but also DBase files. Pretty easy solution.
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Many thanks. JDBC is actually the best way to link with DBF's

I looked a lot for a JDBC driver, but all I could find are paid.

In your example, you've used Caigen DBF driver. I've seen those also, but as they do not apear to be free, I never touched them. :)

Anyway in this new example, there apear to be an error with the form. When running I get

MainForm.RootPane.LoadLayout("Main") - line trowing the error

error

javafx.fxml.LoadException: Invalid property.
at javafx.fxml.FXMLLoader$PropertyElement.<init>(FXMLLoader.java:1169)




again, I would like to thank you your examples. I'm sure they will be extremly usefull for lots of B4J members, myself included.

UPDATE - My mistake, No error in form...I just had to update to JDK8

JM
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Thanks.
I created a code snippet to read the file with GBK charset as in the test project.
 
Upvote 0
Top