Results 1 to 10 of 10
- 04-05-2012, 03:52 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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.
- 04-05-2012, 04:19 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 04-09-2012, 07:02 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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.
- 04-09-2012, 07:40 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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.
- 04-11-2012, 10:58 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 04-11-2012, 11:07 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Constructing the Database Connection String Dynamically at Run Time
That's not possible...I have a vast project.
- 04-11-2012, 12:05 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 04-11-2012, 12:20 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Constructing the Database Connection String Dynamically at Run Time
it is part of object...
- 04-11-2012, 04:34 PM #9
Member
- Join Date
- Apr 2011
- Posts
- 69
- Rep Power
- 0
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...
- 04-11-2012, 06:11 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
Similar Threads
-
JAVA MAIL Connection Time Out ...
By cyberaa in forum New To JavaReplies: 5Last Post: 07-13-2011, 11:00 AM -
How to find connection string of Database?
By vishvadeepaktripathi in forum JDBCReplies: 0Last Post: 02-25-2011, 08:47 AM -
How to create object dynamically with class name known in string format
By ranu_gokhe in forum Advanced JavaReplies: 1Last Post: 04-09-2008, 02:15 AM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:22 PM -
Calculating the DB connection time
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks