choosing a database file

Posted on 2005-04-10
Last Modified: 2010-03-31
Hi Experts,

Here is basically how i am connecting to my database by hardcoding the database name
Instead of hardcoding, the user would like to have the ability to decide what database to use
"Students1.mdb", "Students2.mdb", "Students3.mdb", ...
Any Idea? how i can do this.
Note that all the user will have while using the application is the executable( .bat or .jar file)
I was thinking about using OpenFile dialog but since the user will start the application with an executable, i wonder if it is possible do this.

public class MyClass{
         //Connect to ess database.  
            url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" +
            accessCon = DriverManager.getConnection( url );
            output.append("Connection successful\n");
        catch( SQLException sqlex )
            //process SQLException
            output.append("Connection Uncessful\n" + sqlex.toString() );
          catch( Exception ex )
            //process remaining Exception here
            output.append( ex.toString() );
}//end of MyClass
Question by:komlaaa
    LVL 92

    Accepted Solution

    You could allow the user to choose from a list, or using JFIleChooser.
    Then based on what they selected create the appropriate url for the connect string
    LVL 30

    Assisted Solution

    LVL 16

    Expert Comment

    Another way, is using command-line arguments.

    A starting class must has a main method. This method can receives the command-line arguments from the runtime.
    You can send the class a database filename while executing. See my sample.

    public class MyClass
      protected String dbFile;
      public MyClass(String dbFile)
        this.dbFile = dbFile;
        System.out.println("Database filename is "+dbFile);
      public static voif main(String[] args)
        String dbFile = null;
        if(args.length > 1)
          dbFile = args[0];
        new MyClass(dbFile);

    Assume that you're executing the class by a .BAT file named run.bat.

    // run.bat
    java MyClass "C:/accessDb/Students1.mdb"

    So, you can send the desired database filename to the class, and use it afterward.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now