Results 1 to 8 of 8
  1. #1
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default 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:

    Java Code:
    "jdbc:odbc:Driver={Microsoft Access Driver "
                + "(*.mdb, *.accdb)};DBQ=C:\\Users\\Nephos\\Test.accdb"
    Thanks! :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Change this:
    Java Code:
    "jdbc:odbc:Driver={Microsoft Access Driver "
                + "(*.mdb, *.accdb)};DBQ=C:\\Users\\Nephos\\Test.accdb";

    To this:
    Java 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.

  3. #3
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Thanks ozzyman. I guess that approach could work. :)

    Does anyone know of a way to add a username/password to an access database please?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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

  5. #5
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    Sorry to bump this thread, but how could I write a method to set the DB_PATH if it is static? Cheers :)

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    whether static or not static, creating a get/set method would retrieve/set the value for you.

    Java Code:
    public void setDBPath(String newPath) {
        //might be a good idea to have some validation before you set this
        DB_PATH = newPath;
    }

  7. #7
    nephos is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default

    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. :(

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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

Similar Threads

  1. Basic of database, please
    By RichersooN in forum JDBC
    Replies: 2
    Last Post: 01-20-2011, 05:09 AM
  2. Basic JSP question..
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-28-2009, 07:51 PM
  3. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:09 PM
  4. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •