B4J Code Snippet Opening MS Access databases (mdb)

1. Download UCanAccess: https://www.b4x.com/b4j/files/ucanaccess.zip
(Sources: https://jackcessencrypt.sourceforge.io/ and http://ucanaccess.sourceforge.net/site.html)
The libraries are licensed with Apache 2.0 license: http://www.apache.org/licenses/LICENSE-2.0

2. Copy the jars to the additional libs folder.
3. Add:
B4X:
#AdditionalJar: ucanaccess-5.0.0
#AdditionalJar: commons-lang3-3.8.1
#AdditionalJar: commons-logging-1.2
#AdditionalJar: hsqldb-2.6.1
#AdditionalJar: jackcess-3.0.1-B4J
#IgnoreWarnings: 15
4. There are four text files inside the zip file. Add them to the project Files folder.
5. Open the database:
B4X:
'SQL object from jSQL library
sql.Initialize("net.ucanaccess.jdbc.UcanaccessDriver", "jdbc:ucanaccess://C:/Users/H/Downloads/1.accdb") 'change path as needed
 
Last edited:

Sabotto

Well-Known Member
Licensed User
Here's a demo for accessing .mdb's on Android: https://www.b4x.com/android/forum/t...emo-works-under-b4j-see-notes.121546/#content
If you're looking for accessing encrypted .mdb's on Android, then the above code may work on Android. What you need to watch out for is the library versions that are compatible with Android. For example, UCanAccess is currently at version 5.0.0. This version will not work under Android, you'll have to use 4.0.4.
I would like to access a db access with B4A.
I have read the suggested post, but there is only the code and not the libraries.
Where can I find version 4.0.4? If I go to http://ucanaccess.sourceforge.net/site.html find the last one (the version 5.0.1) which, if I understand correctly, would not work with B4A.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User

Pesciolina

Active Member
Licensed User
Longtime User
Hi BigBoss123,

Please find my code structure below.

B4X:
#Region  Project Attributes
    #MainFormWidth: 800
    #MainFormHeight: 600
    ' SQLite driver                            
      #AdditionalJar: ucanaccess-5.0.0
    #AdditionalJar: hsqldb-2.5.0
    #AdditionalJar: jackcess-3.0.1-B4J
    #AdditionalJar: jackcess-encrypt-3.0.0
    #AdditionalJar: commons-logging-1.2
    #AdditionalJar: commons-lang3-3.8.1
    #AdditionalJar: bcprov-jdk15on-166
    #AdditionalJar: jaxb-api-2.3.0
    #AdditionalJar: jaxb-impl-2.1
    #AdditionalJar: activation-2.2.1
    #IgnoreWarnings: 15
#End Region

Variables declared in Process_Globals:
B4X:
    Private gSQL As SQL
    Private DBFile As String
    Dim sSQL As String
    Dim sDBpass As String

Function to Initialize the DB:
B4X:
Sub Database_Init As Boolean
    Dim Result As Boolean = False
   
    DBFile = File.DirData("PKDBServer") & "\" & "users.accdb"
    sDBpass = "passpass"
    sSQL = "jdbc:ucanaccess://" & DBFile & ";jackcessOpener=b4j.PKDBServer.main$MyOpener;memory=false;ignorecase=true;encrypt=true;COLUMNORDER=DISPLAY;singleconnection=true"
   
    Try
        Log(" Init sql connection...")
        gSQL.Initialize2("net.ucanaccess.jdbc.UcanaccessDriver", sSQL,"", sDBpass)
        Result = True
    Catch
        Log(LastException.Message)
    End Try
    Return Result
End Sub

JAVA code at the end of the MAIN page:
B4X:
#If Java
    import java.io.File;
    import java.io.IOException;
    import net.ucanaccess.jdbc.JackcessOpenerInterface;
    import com.healthmarketscience.jackcess.CryptCodecProvider;
    import com.healthmarketscience.jackcess.Database;
    import com.healthmarketscience.jackcess.DatabaseBuilder;
    import java.sql.SQLException;

    public static class MyOpener implements net.ucanaccess.jdbc.JackcessOpenerInterface {
        public Database open(File fl,String pwd) throws IOException {
           Database db = new DatabaseBuilder(fl).setCodecProvider(new CryptCodecProvider(pwd)).open();
           return db;
        }
    }
   
#End If
Hi,
this code, which would be very useful, gives me this error:


B4X:
B4J Versione: 10.30
Analisi del Codice.    (0.00s)
    Java Versione: 19
Building folders structure.    (0.02s)
Compilazione del codice.    (0.02s)
Compilazione del codice di layouts    (0.00s)
Organizzazione Librerie.    (0.00s)
Compilazione del codice Java prodotto.    Error
src\b4j\example\main.java:7: error: cannot find symbol
    import com.healthmarketscience.jackcess.CryptCodecProvider;
                                           ^
  symbol:   class CryptCodecProvider
  location: package com.healthmarketscience.jackcess
1 error
only showing the first 1 errors, of 2 total; use -Xmaxerrs if you would like to see more

javac 19.0.2

can you help me?
 
Top