-
Basic Database Question
I have created a simple application in NetBeans which connects to a MS Access database located on the local machine. My problem comes when I want to run this program on different machines as the URL of the database changes for each machine. Is there a way so that I no longer need to change the location of the database in my code for each machine i use the program on? My code looks something like this:
Code:
"jdbc:odbc:Driver={Microsoft Access Driver "
+ "(*.mdb, *.accdb)};DBQ=C:\\Users\\Nephos\\Test.accdb"
Thanks! :)
-
Change this:
Code:
"jdbc:odbc:Driver={Microsoft Access Driver "
+ "(*.mdb, *.accdb)};DBQ=C:\\Users\\Nephos\\Test.accdb";
To this:
Code:
public static String DATABASE_PATH = "C:\\Users\\Nephos\\Test.accdb";
"jdbc:odbc:Driver={Microsoft Access Driver "
+ "(*.mdb, *.accdb)};DBQ=" + DATABASE_PATH;
Then you can add a method to set a new path, or open a file browser to allow the user to choose a database file from their hard drive.
-
Thanks ozzyman. I guess that approach could work. :)
Does anyone know of a way to add a username/password to an access database please?
-
In Access 2007 go to the "Database Tools" Tab, there is a button called "Encrypt with password" just click it. I think in the older Access versions its under a Security menu in an Options menu on the toolbar.
Btw Access related questions don't belong here as this is a java forum
-
Sorry to bump this thread, but how could I write a method to set the DB_PATH if it is static? Cheers :)
-
whether static or not static, creating a get/set method would retrieve/set the value for you.
Code:
public void setDBPath(String newPath) {
//might be a good idea to have some validation before you set this
DB_PATH = newPath;
}
-
I initialized DB_PATH to null then tried to set it with a method, but when it connects to the DB I get a NullPointerException which seems it's not actually changing it. :(
-
don't set it to null, set it to the default path, and surround with try { } catch (NullPointerException e) {} so you can handle the error and use the default path in case the user sets it to a bad file path