JDBC connection with configuration file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • js06am
    New Member
    • Feb 2007
    • 8

    #1

    JDBC connection with configuration file

    Hi.

    Is it possible to connect to a database using JDBC with a configuration file? I understand and can implement the alternate methods such as "Class.forName( "...."); " and "Connection connection = DriverManager. getConnection(" .....");" etc, but I am struggling to come up with a solution for a configuration file (which I have, which specifies the driver and database url.

    Any tips or reading recommendations would be greatly appreciated.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by js06am
    Hi.

    Is it possible to connect to a database using JDBC with a configuration file? I understand and can implement the alternate methods such as "Class.forName( "...."); " and "Connection connection = DriverManager. getConnection(" .....");" etc, but I am struggling to come up with a solution for a configuration file (which I have, which specifies the driver and database url.

    Any tips or reading recommendations would be greatly appreciated.
    Yep, you can do that; that's what Properties objects are for: read a .properties
    file in a Properties object and consult it for your configuration strings.

    kind regards,

    Jos

    Comment

    • js06am
      New Member
      • Feb 2007
      • 8

      #3
      If I read my configuration file into a Properties object, how would you then use this properties object to specify a driver and connection? Can this be written as a simple line like the other methods of "Connection connection - DriverManager.g etConnection(.. ." etc or would you need to write a small method?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by js06am
        If I read my configuration file into a Properties object, how would you then use this properties object to specify a driver and connection? Can this be written as a simple line like the other methods of "Connection connection - DriverManager.g etConnection(.. ." etc or would you need to write a small method?
        You have to write a small method: it should consult the Properties object and
        retrieve a value String given a key String, say, "jdbc.drive r", "jdbc.user" and
        "jdbc.passw ord" or similar. Given the value Strings it should install the driver
        and do whatever it must do.

        kind regards,

        Jos

        Comment

        Working...