B4J Question B4J and Database ACCESS ?

marco.canta

Active Member
Licensed User
Longtime User
Good evening, I am not able to open and manage file access, nor with it Jackcess library - JDBC driver, someone has done it?
Example ??
Thanks !!!!
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

have explored. Pls find below and attached a sample project. Copy the cards.mdb to the files folder after compile.

To connect to databases via JDBC, you need the JDBC Driver and JDBC URL Format.

-General Information
Download the driver from the reference site.
Copied the driver to the additional libraries folder: Menu Tools > Configure Path > Additional Libraries
Add the driver(s) to the Project Attributes as #AdditionalJar.
Reference the jSQL Library in the B4J IDE Libs Tab.

Look here: http://docs.oracle.com/javase/tutorial/jdbc/ to learn more about JDBC.

-MS Access Database
Information & Download:
http://ucanaccess.sourceforge.net/site.html

ADDITIONAL JAR:
#AdditionalJar: ucanaccess-2.0.2.jar
#AdditionalJar: hsqldb.jar
#AdditionalJar: jackcess-2.0.3.jar
#AdditionalJar: commons-logging-1.0.4.jar
#AdditionalJar: commons-lang-2.6.jar


JDBC DRIVER CLASS:
net.ucanaccess.jdbc.UcanaccessDriver

JDBC URL FORMAT:
jdbc:ucanaccess://<Path to Databasemdb>;memory=true

Example:
Private gSQL As SQL
gstrDBFile = File.DirApp & "\cars.mdb"
gSQL.Initialize("net.ucanaccess.jdbc.UcanaccessDriver", "jdbc:ucanaccess://" & gstrDBFile & ";memory=true")
DBUtils.ExecuteTableView(gSQL, "SELECT * FROM cars", Null, 0, tvwMain)
 

Attachments

  • b4jhowotaccessdb.zip
    34.7 KB · Views: 756
Upvote 0

manios

Active Member
Licensed User
Longtime User
I tried to run the sample but got the following error
B4X:
Program started.
AppInit :: Start
Database file: C:\DOKUME~1\XPMUser\EIGENE~1\B4J\B4JHOW~1\B4JHOW~1\Objects\cars.mdb
Init sql connection...
java.lang.RuntimeException: java.lang.IncompatibleClassChangeError: Found class com.healthmarketscience.jackcess.Database, but interface was expected
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:69)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at b4j.example.main.start(main.java:33)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IncompatibleClassChangeError: Found class com.healthmarketscience.jackcess.Database, but interface was expected
    at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:147)
    at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:57)
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:100)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at anywheresoftware.b4j.objects.SQL.Initialize2(SQL.java:48)
    at anywheresoftware.b4j.objects.SQL.Initialize(SQL.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:504)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:215)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:153)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    ... 14 more
Any suggestion where to search?
Manios
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi Manios,

have you downloaded the msaccess drivers from the reference site & installed into your additional libraries folder?

MS Access Database Driver - copy to B4J additional libraries folder
Download from here.

MS Access Example attached developed with B4J 6.30, Layout with the B4J Internal Designer.
 

Attachments

  • B4JHowToMSAccessv2.zip
    42.3 KB · Views: 443
Last edited:
Upvote 0

manios

Active Member
Licensed User
Longtime User
Hi rwblinn,
after download both the drivers and the example it does work now. I only had to manually copy the cars.mdb in to the Objects directory.
Thanks for your help!
Manios
 
Upvote 0

manios

Active Member
Licensed User
Longtime User
I tried to add a record to a table with DBUtils.InsertMaps, however nothing is added and also no error message.
Is it possible the drivers does not support writing into the database or did I miss something?

Manios
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi Manios,

the data is correctly added to the database, but you need to refresh the data in the tableview.

Post #10: updated MS Access Example showing insert and delete with tableview refresh.
Note: The tableview refresh method used is not the best solution, but it works.
 
Last edited:
Upvote 0

manios

Active Member
Licensed User
Longtime User
Hi rwblinn,

well I checked my database with a second program and with Access there are no records added. The first column of the table is an autoincrement field, could this be the problem? I will check later with a table without autoincrement field and will let you know.

Manios
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
@manios: Could you ever clear this problem - i have exactly the same problem. I updated to the current versions, tried to remove autoincrement field etc. I get a record, but no field values inside. I also dont get any error message; lastexception is not used.
 
Upvote 0

panoss

Member
Hi Manios,

have you downloaded the msaccess drivers from the reference site & installed into your additional libraries folder?

If not, pls find the drivers and an update of the example project:

MS Access Database Driver - copy to B4J additional libraries folder
http://www.rwblinn.de/b4j/dbdrivers/dbdrivermsaccess.zip

MS Access Example
http://www.rwblinn.de/b4j/b4jhowto/examples/b4jhowtomsaccess.zip

Hi everybody.
I 'm looking for examples B4J + MsAccess db but the above links don't work.
Are there any examples that I could download?
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi everybody.
I 'm looking for examples B4J + MsAccess db but the above links don't work.
Are there any examples that I could download?

Hi,

updated post #10 - note that you need to download the MS Access Database Driver from here.
Please note: have not tested the latest drivers from the link.
 
Upvote 0

panoss

Member
Thanks rwblinn!
After a few modifications it worked!
If there are any other examples with (MsAccess or SQLite) databases + b4j they 're welcome.
 
Last edited:
Upvote 0

panoss

Member
I have opened the project B4JHowToMsAccess in the IDE.
I open the designer (Designer->Open internal designer) and I get an error:

B4JSystemOutOfMemory.png

'OutOfMemoryException'.
I read here it 's due to a corrupted layout file.
How can I fix it?
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
I have opened the project B4JHowToMsAccess in the IDE.
I open the designer (Designer->Open internal designer) and I get an error:
View attachment 70345

The original example is from the early days of B4J (back in 2014) using FXML layouts. FXML layouts are not recommended anymore (deprecated).

Updated the original example in post #10 with a version using a B4J Internal Designer created layout.
 
Upvote 0
Top