File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes java action Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java action Class" Watch "java action Class" New topic
Author

java action Class

Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
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
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
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

Joined: Aug 26, 2008
Posts: 263
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

SCJP 1.5(97%) My Blog
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 150
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

Joined: Jun 22, 2009
Posts: 150
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

Joined: Aug 26, 2008
Posts: 263
no problem Mahi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java action Class
 
Similar Threads
JSP and MySql
whats wrong here in connection string
Where to write Validation
What should be the connection URL
MySQL Login Form