Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java action Class

 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a website developed on struts frame work..
In my action class I have used Connection conurl=("jdbc:mysql://localhost:3306/DB_Name?"+"user=root&password=pwd");
Now my dout is when I host my website should i change the ://localhost:3306/DB_Name?"+"user=root&password=pwd" according the services providers.. If so should I change it for every action class.

Thank you in advance

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should refactor the code so that the DB connection parameters are not hardcoded, but instead read from an external config file. That way, you only need to change the file, but not anything in the source code.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:You should refactor the code so that the DB connection parameters are not hardcoded, but instead read from an external config file. That way, you only need to change the file, but not anything in the source code.



Sorry I didnt get you.. I am new to this.. What is external config file. Please help..

 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should define your database connection parameters in struts-config.xml configuration file in <data-sources> tag. once you do this you can directly access your database connection in action using getDataSource(request) method. Advantage of this approch is, whenever you change your database no need to change DB parameters in all the action classess where you are accessing the DB but you have to make DB parameter changes only in configuration file.

Please go through the link for sample code
http://struts.apache.org/1.2.x/faqs/database.html
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34681
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi,
If you aren't using Struts, Ulf is just referring to a ".properties" file. It's a file with name=value pairs. That way you can change the configuration without having to recompile the code.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shivendra tripathi wrote:You should define your database connection parameters in struts-config.xml configuration file in <data-sources> tag. once you do this you can directly access your database connection in action using getDataSource(request) method. Advantage of this approch is, whenever you change your database no need to change DB parameters in all the action classess where you are accessing the DB but you have to make DB parameter changes only in configuration file.

Please go through the link for sample code
http://struts.apache.org/1.2.x/faqs/database.html



Thank you for the reply. The link really helped me.

 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Mahi,
If you aren't using Struts, Ulf is just referring to a ".properties" file. It's a file with name=value pairs. That way you can change the configuration without having to recompile the code.



Thank you ..

 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no problem Mahi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic