[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


choosing a database file

Posted on 2005-04-10
Medium Priority
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

objects earned 800 total points
ID: 13750252
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

by:Mayank S
Mayank S earned 200 total points
ID: 13750644
LVL 16

Expert Comment

ID: 13751348
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.

// MyClass.java
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

872 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