Results 1 to 10 of 10
  1. #1
    ajaxjagadish1 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Constructing the Database Connection String Dynamically at Run Time

    Hello,

    Im developing a project where in the connection to DataBase is static i.e Im hard coding the details of the database in createConnection() method.
    Now Im required to dynamically connect to a database i.e in the first page of my project, the user enters the database details(servername,port,username,passowrd,servicen ame) and using this details my project should be connected to respective database.

    One solution to this is to have my createConnection() method as createConnection(String serverName,String portNumber,
    String userName,String pword,String s_id).
    But my project is very vast and everywhere I have used createConnection() to connect to database, changing this method to createConnection(String serverName,String portNumber,
    String userName,String pword,String s_id) everywhere is not possible.
    So im looking for an alternative solution.
    Thanks in Advance :-)

    P.S: The project may be accessed by more than one user, so I cannot store the database details in a file and then use this file to retrieve the information.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    What sort of app is this?
    Desktop, web?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ajaxjagadish1 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    Hi Tolls, Thanks for the reply...It's a Web application developed using struts framework.

  4. #4
    ajaxjagadish1 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    My web application connects to a database. I have a DAOFactory class where in I have createConnection() method that connects to the database. I have many classes in my project which uses this method to connect
    to the database. Now I have been given a requirement like, my application should connect to the database to which the user wants to connect.
    So now the first page of my project is a form wherein the user will enter the Database information(servername,port,username,passowrd,sid) .
    Depending on this info provided by the user my project should work on this DB which the user has given the details about.

    Now, since I have to connect to database which user want I need to have the createCOnnection() method as
    createConnection(String serverName,String portNumber,String userName,String pword,String s_id)
    but changing replacing the former with the latter in the project is not possible. So im looking for a solution for this which should work even when multiple users acces my application.


    P.S: Static variables cannot be used to store the servername,port,username,passowrd and sid since this case would fail when multiple users acces my application.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    Why can't you change the method signature?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ajaxjagadish1 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    That's not possible...I have a vast project.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    Where is the createConnection() method?
    Is it a static method or is it part of an object?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ajaxjagadish1 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    it is part of object...

  9. #9
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    ever heard of refactoring? It wouldn't be THAT difficult to change all your methods...

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Constructing the Database Connection String Dynamically at Run Time

    And that object is created when?
    You;re going to have to give us more of a design here, as otherwise this could take some time to get to a solution.

    If you create a DB-connection-making-object once per session then simply give that class a constructor that takes those connection parameters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JAVA MAIL Connection Time Out ...
    By cyberaa in forum New To Java
    Replies: 5
    Last Post: 07-13-2011, 11:00 AM
  2. How to find connection string of Database?
    By vishvadeepaktripathi in forum JDBC
    Replies: 0
    Last Post: 02-25-2011, 08:47 AM
  3. Replies: 1
    Last Post: 04-09-2008, 02:15 AM
  4. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:22 PM
  5. Calculating the DB connection time
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:55 AM

Tags for this Thread

Posting Permissions

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