0

I'm working on a project for my intership and i would like to connect my ms access database to an android app on a tablet. But this tablet cannot access to web services only the intern network of the company.

I'm developing my app with android studio an try to use ucanaccess driver, but that didn't work. I'm trying to resolve the problem with the new librairy you advise to me.

I download ucanaccess 5.0.1 and hsqldb 2.0.5 from https://mvnrepository.com/artifact/net.sf.ucanaccess/ucanaccess/5.0.1 ucanaccess 5.0.1 and hsqldb 2.0.5 which i add to the libs files from my project then i did the implementation in gradle.

But when i run, my app crash, caused by java.lang.NoClassDefFoundError: org.hsqldb.jdbc.JDBCDriver$EmptyDiverAction

According to the runtime the error comme from to Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); but i don't understand why and how to resolve it

my main

package fr.Emka.Banc_test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "ENR";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner _Client_ref = (Spinner) findViewById(R.id.client_ref);      /*déclaration spinner pour client_ref*/
        Spinner _Ref = (Spinner) findViewById(R.id.ref_vente);
        Spinner Client_banc = (Spinner) findViewById(R.id.client_banc);
        Spinner Banc = (Spinner) findViewById(R.id.banc);

        List<String> listSpinClientRef = new ArrayList<String>();
        List<String> listSpinRef = new ArrayList<String>();
        List<String> listSpinClientBanc = new ArrayList<String>();
        List<String> listSpinBanc = new ArrayList<String>();

        listSpinRef.add("Sélectionner une Référence");
        listSpinClientRef.add("Sélectionner un Client");
        listSpinClientBanc.add("Sélectionner un Client");
        listSpinBanc.add("Sélectionner un Banc");

        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("class ucanaccess fail");
        }
        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("class hsqldb fail");
        }
        //connection to access database

        try {
            Connection base = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/lcor/AndroidStudioProjects/Banc_test/Banc_test.accdb");
            System.out.println("Connected to MS Access database");
            Log.d(TAG,"Connected to MS  Access database");
            //query select records
            String SQL = "SELECT DISTINCT Client FROM Liste_bancs";
            ResultSet result = base.createStatement().executeQuery(SQL);
            //loop implement record in spinner
            while (result.next())
            {
                String client = result.getString("Liste_bancs");
                listSpinClientRef.add(client);
            }

            base.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
            System.out.println("connection fail");
        }

        listSpinClientRef.add("emka");
        ArrayAdapter<String> spin_Client_ref = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,listSpinClientRef);
        spin_Client_ref.setDropDownViewResource(android.R.layout.simple_spinner_item);
        _Client_ref.setAdapter(spin_Client_ref);

        ArrayAdapter<String> spin_ref = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,listSpinRef);
        spin_ref.setDropDownViewResource(android.R.layout.simple_spinner_item);
        _Ref.setAdapter(spin_ref);

        ArrayAdapter<String> spin_Client_banc = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,listSpinClientBanc);
        spin_Client_banc.setDropDownViewResource(android.R.layout.simple_spinner_item);
        Client_banc.setAdapter(spin_Client_banc);

        ArrayAdapter<String> spin_Banc = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,listSpinBanc);
        spin_Banc.setDropDownViewResource(android.R.layout.simple_spinner_item);
        Banc.setAdapter(spin_Banc);
    }

}

my gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "fr.Emka.Banc_test"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:2.0.1'
    implementation group: 'net.sf.ucanaccess', name: 'ucanaccess', version: '5.0.1'
    testImplementation group: 'org.hsqldb', name: 'hsqldb', version: '2.5.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

error log

06/10 12:36:20: Launching 'app' on ViewSonic VSD242.
Install successfully finished in 26 s 52 ms.
$ adb shell am start -n "fr.Emka.Banc_test/fr.Emka.Banc_test.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 4879 on device 'viewsonic-vsd242-AUK0B4J10B'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/PhoneWindow: DecorView - SCREEN_WEITH = 1920 - SCREE_HEIGHT = 1032
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
    Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
W/art: Suspending all threads took: 10.594ms
I/art: Background partial concurrent mark sweep GC freed 683(103KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 2MB/4MB, paused 12.148ms total 28.881ms
I/ListPopupWindow: Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.
I/art: Rejecting re-init on previously-failed class java.lang.Class<org.hsqldb.jdbc.JDBCDriver$EmptyDiverAction>
I/art: Rejecting re-init on previously-failed class java.lang.Class<org.hsqldb.jdbc.JDBCDriver$EmptyDiverAction>
I/art: Rejecting re-init on previously-failed class java.lang.Class<org.hsqldb.jdbc.JDBCDriver$EmptyDiverAction>
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: fr.Emka.Banc_test, PID: 4879
    java.lang.NoClassDefFoundError: org.hsqldb.jdbc.JDBCDriver$EmptyDiverAction
        at org.hsqldb.jdbc.JDBCDriver.<clinit>(Unknown Source)
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:309)
        at java.lang.Class.forName(Class.java:273)
        at fr.Emka.Banc_test.MainActivity.onCreate(MainActivity.java:42)
        at android.app.Activity.performCreate(Activity.java:6222)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2407)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5280)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
I/Process: Sending signal. PID: 4879 SIG: 9

a picture of where are my lib and the dependencies enter image description here

lcorn
  • 11
  • 3
  • Hey there, could you also share how the dependencies look like in Android Studio? Another thing you can try is if the driver name is case-sensitive, it tries to to find something `UCanAccess`, but the documentation suggests that it should be lower-case http://ucanaccess.sourceforge.net/site.html – thinkgruen Jun 09 '21 at 07:39
  • Which build tool are you using: `gradle` or `maven`? please share also build tool configuration. – oleg-sta Jun 09 '21 at 07:59
  • I'm using gradle i added a picture of the dependendies. Before i tested with lower-case `ucanaccess` and i had the same error. – lcorn Jun 09 '21 at 08:14
  • hi @Icorn you need to add one library only to access the database. Gradle: `implementation group: 'net.sf.ucanaccess', name: 'ucanaccess', version: '5.0.1' ` https://mvnrepository.com/artifact/net.sf.ucanaccess/ucanaccess/5.0.1 Did you create the connector settings to the database? This answer might be resolved your problem https://stackoverflow.com/questions/21955256/manipulating-an-access-database-from-java-without-odbc/21955257#21955257 – randytan Jun 09 '21 at 08:18
  • @randytan i add your librairy and did the implementation but now i have the following error `java.lang.NoClassDefFoundError: net.ucanaccess.jdbc.UcanaccessDriver` and the app crash. So I add `Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");` I compile then had a new error `java.lang.NoClassDefFoundError: org.hsqldb.jdbc.JDBCDriver`. Do you know why ? – lcorn Jun 09 '21 at 10:25
  • NoClassDefFoundError means that the class is not present at runtime, so since this is not occurring for UcanaccessDriver anymore, this seems fixed. The next problem seems to be that you have both an UcanaccessDriver AND the hsqldb driver present according to your dependencies, so the problem moves just to the next driver. – thinkgruen Jun 09 '21 at 19:20
  • @Icorn, so then now your library and Class already set to run on runtime, Next, the hsqldb.jdbc, looks like it is unable to find your hsqldb, did you extract and put to your gradle settings? – randytan Jun 10 '21 at 05:35
  • @randytan I delete my old library and add the uacanaccess.jar and hsqldb.jar and implement as it's said on maeven website but that always crash. I only put inside libs file from my project and set gradle as say from maven website – lcorn Jun 10 '21 at 07:08
  • @Icorn what error you got during crash on the console log? – randytan Jun 10 '21 at 09:55
  • @randytan i have add the log in the post, the error it's `java.lang.IllegalStateException: No current tap-and-pay account` – lcorn Jun 10 '21 at 10:42
  • @Icorn, the crash error is unrelated with the original question, please close this topic and raise another one. Thanks, – randytan Jun 10 '21 at 10:55
  • @randytan i do it, this is better? – lcorn Jun 10 '21 at 11:07

0 Answers0