2

I have written a simple program for myself which works with an Access database file. I used the absolute path to connect to the db:

String path = "jdbc:ucanaccess://D:/Development/20_Eclipse/Budget/data/Budget.accdb";
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        this.conn = DriverManager.getConnection(path);

When I use the runnable jar somewhere else this path (understandably) doesn't work. So I tried relative paths but they didn't work either.

Then I tried to build the path using

String path = "jdbc:ucanaccess://" + helper.programmPathForDB() + "/data/Budget.accdb";

where helper.programPathForDB() does this:

public String programmPathForDB()
{
    String tempPath = this.getClass().getResource( "." ).toString();
    String path = tempPath.substring(6, (tempPath.length()-12));

    return path;
}

This works ONLY when I start the program through my IDE (Eclipse). When I start the jar somewhere else it doesn't work.

How can I resolve this issue?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Mattizin
  • 350
  • 1
  • 6
  • 19

3 Answers3

1

It sounds like you want to have the database file in a folder that is relative to (for example, a subfolder of) the location of the JAR file. If so, then

Get location of JAR file

offers some suggestions on how to determine location of the JAR file.

However, you should bear in mind that it might not be such a good idea to put the database file there. Depending on how the application is deployed, such a location may deny write access to users without elevated privileges (e.g., %ProgramFiles% under Windows). Also, moving the JAR file without also moving the database file could break your application.

All modern operating systems provide well-known and discoverable locations where applications can safely put "stuff" that is not an integral part of the (executable) application itself. For example, Windows offers %APPDATA% and %LOCALAPPDATA% for user-specific locations, along with %PUBLIC% for resources that need to be shared among all users. You should consider using (a subfolder of) one of those locations for your database file.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • thank u so much, i will take a look into ur link tomorrow. I got some problems installing my project on some devices. With ur explanation about the elevated privileges it makes sense, cause i treied to install all inclusive db to ProgramFiles. When i have the DB under APPData The Path to it will be the same on every windows pc or? – Mattizin Feb 17 '16 at 21:18
  • The absolute path might not be the same on every machine, but every machine will have an environment variable that can be used to determine the actual path. That's what I meant when I used the term "discoverable" above. – Gord Thompson Feb 18 '16 at 05:00
1

So with the help of Gord Thompson above i solved it with deploying the DB under APPDATA And using the Envirement Variable + my Project path:

String path = "jdbc:ucanaccess://" + System.getenv("APPDATA") + "/Budget/data/Budget.accdb";
Mattizin
  • 350
  • 1
  • 6
  • 19
1

The UCanAccess connection URL must always begin with jdbc:ucanaccess://, followed by the path to the database file.

For a relative path (e.g., src/main/resources/testDB.accdb) that would be

jdbc:ucanaccess://src/main/resources/testDB.accdb