1

I have an android app that uses Microsoft Access database. when I try to run the app I get this following error:

W/System.err: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::0.0 given file does not exist: C:\Users\User\Documents\tets.accdb

but when you go to the file location the file exists. I've tried changing the file path and for the most part, it didn't work though sometimes it will give me another error:

for this file path String url = "jdbc:ucanaccess:/C:/Users/User/Documents/tets.accdb";

it gives me this error:

W/System.err: java.sql.SQLException: No suitable driver found for jdbc:ucanaccess:/C:/Users/User/Documents/tets.accdb

I can't seem to understand the difference between the URLs or what is causing this problem. Does anybody know what is causing this issue?

the full code:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import java.sql.*;
public class Books extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_books);
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:ucanaccess://C:\\Users\\User\\Documents\\tets.accdb";
        try {
            Connection con = DriverManager.getConnection(url);
            Statement statement = con.createStatement();
            ResultSet res =statement.executeQuery("SELECT * FROM tets");
            while(res.next()){
                System.out.println(res.getInt(1));
            }
            con.close();
            statement.close();
            res.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
shosho155
  • 21
  • 3

2 Answers2

0

Your Android app runs in an emulator/device without access to file-system of IDE host. To access a file you have to import it to emulator/device:

In Android Studio 3.0 and later do this: View > Tool Windows > Device File Explorer

See How to access local files of the filesystem in the Android emulator?

ComputerVersteher
  • 2,638
  • 1
  • 10
  • 20
  • is this necessary if the database location is the app assets folder? – shosho155 Aug 09 '20 at 11:12
  • No, but you need to use URI relative to emulator, what is not the windows path. See https://stackoverflow.com/questions/4820816/how-to-get-uri-from-an-asset-file – ComputerVersteher Aug 09 '20 at 12:08
  • if I understand you correctly `jdbc:ucanaccess://file:///android_asset/tets.accdb` is the path that I need? – shosho155 Aug 09 '20 at 14:18
  • Too bad, any reason you don't use Androids default SQLite database? Not sure if you can use ucanaccess on Android as there are no samples, only for java on other platforms. – ComputerVersteher Aug 09 '20 at 20:15
-1

Make sure it is the correct name of the path you misspelled the word test and wrote tets maybe that is the problem?

  • @shosho155 okay you can keep the 2 `/` but make sure that tets.accdb is actually there. maybe it is called `test` instead of `tets`? – Youssof Elgohary Aug 08 '20 at 16:58