0

I have tried to connect to an Access database, but no matter what I try I keep getting the same error:

UCAExc:::0.0 given file does not exist:

I have been using UCanAccess with Android Studio v2.3. I have tried YouTube and I have searched everywhere, but no code snippet is working.

Does this have to do with that I am using windows 10 and I have to place the database in a special folder?

I have included the Error log at the end of the code

package com.example.david.dvd2;
// * Created by David on 2017-04-14.

import java.io.StringWriter;
import java.sql.*;
import java.util.Enumeration;
import android.util.Log;
import org.apache.commons.lang.exception.ExceptionUtils;

public class TestConnection {

public static void CreateDBConection() {
    Log.i("TESTConnection","Inside TestConnection [CreateDBConnection]");
    try {
        Statement st;
        ResultSet re;
/*
Layout of the Access Database, a simple database with DVD-Titles
tblDVD      Id, m,d,fldTitle, fldSweTitle, fldTime, fldComment, fktyp, 
            fkyear,
tblType     Id, fldType
tblYear     Id, fldYear
*/
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        String dbFileName = "C:/db/dvd.mdb";
        String database = "jdbc:ucanaccess://" + dbFileName +  "";
        Connection cn = DriverManager.getConnection(database, "", "");
        st = cn.createStatement();
        re = st.executeQuery("SELECT Id, fldYear FROM tblYear WHERE Id=35");
        while (re.next()) {
            String idoo = re.getString("Id");
            String fldyearoo = re.getString("fldYear");
            Log.i("Result from SQL query", "A selected Year with ID=35: " + fldyearoo);
        }
    }
    catch(ClassNotFoundException | SQLException e)
    {
        Log.i("ERROR","Info:\n" + e.getMessage());
        String s = ExceptionUtils.getStackTrace(e);
        Log.i("ERROR","More detailed info\n" + s);
        e.printStackTrace(System.err);
    }
}

}

Here is the Err.log:

    04-21 16:19:04.466 1693-1693/com.example.david.dvd2 I/TESTConnection: Inside TestConnection [CreateDBConnection]
    04-21 16:19:04.554 1693-1700/com.example.david.dvd2 W/art: Suspending all threads took: 9.814ms
    04-21 16:19:04.648 1693-1709/com.example.david.dvd2 I/art: Background sticky concurrent mark sweep GC freed 8515(456KB) AllocSpace objects, 9(288KB) LOS objects, 16% free, 2MB/3MB, paused 6.486ms total 34.758ms
    04-21 16:19:04.680 1693-1709/com.example.david.dvd2 W/art: Suspending all threads took: 32.195ms
    04-21 16:19:04.785 1693-1709/com.example.david.dvd2 W/art: Suspending all threads took: 22.748ms
    04-21 16:19:04.897 1693-1693/com.example.david.dvd2 I/ERROR: Info:
    UCAExc:::0.0 given file does not exist: /C:/db/dvd.mdb
    04-21 16:19:04.933 1693-1693/com.example.david.dvd2 I/BERROR: More detailed info
    net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::0.0 given file does not exist: /C:/db/dvd.mdb
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:264)
    at java.sql.DriverManager.getConnection(DriverManager.java:179)
    at java.sql.DriverManager.getConnection(DriverManager.java:213)
    at com.example.david.dvd2.TestConnection.CreateDBConection(TestConnection.java:26)
    at com.example.david.dvd2.MainActivity.mastermind2(MainActivity.java:20)
    at com.example.david.dvd2.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    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:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Caused by: java.io.FileNotFoundException: given file does not exist: /C:/db/dvd.mdb
    at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:372)
    at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:252)
    at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:35)
    at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:161)
    at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:51)
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:99)
    at java.sql.DriverManager.getConnection(DriverManager.java:179) 
    at java.sql.DriverManager.getConnection(DriverManager.java:213) 
    at com.example.david.dvd2.TestConnection.CreateDBConection(TestConnection.java:26) 
    at com.example.david.dvd2.MainActivity.mastermind2(MainActivity.java:20) 
    at com.example.david.dvd2.MainActivity.onCreate(MainActivity.java:12) 
    at android.app.Activity.performCreate(Activity.java:5990) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    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:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::0.0 given file does not exist: /C:/db/dvd.mdb
    04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err:     at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:264)
    04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:179)
    04-21 16:19:04.933 1693-1693/com.example.david.dvd2 W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:213)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.example.david.dvd2.TestConnection.CreateDBConection(TestConnection.java:26)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.example.david.dvd2.MainActivity.mastermind2(MainActivity.java:20)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.example.david.dvd2.MainActivity.onCreate(MainActivity.java:12)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.Activity.performCreate(Activity.java:5990)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.os.Looper.loop(Looper.java:135)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err: Caused by: java.io.FileNotFoundException: given file does not exist: /C:/db/dvd.mdb
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:372)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:252)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:35)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:161)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:51)
    04-21 16:19:04.934 1693-1693/com.example.david.dvd2 W/System.err:     at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:99)
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • If your database file really is in the root folder of the `C:` drive then your Java code may not have "modify" permissions for that file. Try copying the file to a location where you *know* that you'll have read/write permissions on the file. – Gord Thompson Apr 20 '17 at 14:33
  • I have tried to put the database in another place and set the right permissions but no luck so far. Now I have c:/db/dvdjoke.accdb – NoobACE-Java Apr 21 '17 at 11:34
  • In your `catch` block for SQLException, have your code `e.printStackTrace(System.err);` and then [edit] your question to include the complete stack trace. – Gord Thompson Apr 21 '17 at 13:30
  • I had wrong databasename in this comment, only here, in the code-snippet it is right. It is supposed to be dvd.mdb. I have change it now so it matches the java code-snippet. I have tried to put the database in another place and set the right permissions but no luck so far. Now I have c:/db/dvd.mdb – NoobACE-Java Apr 21 '17 at 18:26
  • The leading slash in `/C:/db/dvd.mdb` is suspicious. If you are running this code in an Android emulator then that environment may not be able to directly access the Windows file system. You may need to do something like [this](http://stackoverflow.com/a/32019146/2144390) before your Android app can manipulate the file. – Gord Thompson Apr 21 '17 at 20:26
  • That was my first thought, Can this be done in Android Studio. Maybe I can push the file onto the emulated device and change the path to the new place where the database is stored. I did believe my code could access my local windows 10 filesystem. I'll get back and let you know of my progress. – NoobACE-Java Apr 22 '17 at 01:50
  • Solved The mainissue was that Android Studio is not allowed to read files on the local developing machine. Once I copied the database to the device then I got it all to work. An adjustment to the path to match the device path was all it took. – NoobACE-Java Apr 22 '17 at 02:35
  • Thanks Gord Thompson for your effort and your suggestions. Good Help – NoobACE-Java Apr 22 '17 at 02:37

1 Answers1

0

Code running under an Android emulator in Android Studio apparently cannot directly access the filesystem of the host. In this case, the Windows path C:/db/dvd.mdb was being interpreted as the Android path /C:/db/dvd.mdb

Therefore we need to import the database file into the filesystem on the Android "device" as explained in this answer.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • The guide from above is for saving from the Device, It is important to use the Push-button to save a file onto the Device. First Start your Device. Navigate to where you want to save the file. Use the Push-button to your upload the file and you are done. Now you can reach the file, in this case the databasefile dvd.mdb. Edit your connection-path to match the place where the dvd.mdb file ended up. – NoobACE-Java Apr 22 '17 at 22:36