my dog learned polymorphism*
The moose likes JSP and the fly likes How to create common database connection in a application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to create common database connection in a application" Watch "How to create common database connection in a application" New topic
Author

How to create common database connection in a application

abhijeet srivastava
Greenhorn

Joined: Aug 22, 2006
Posts: 26
Hi all!


I am using type 4 database driver in my jsp application. I want to make common database connection which will be used in any jsp page in my application. (ie; I do'nt want to write code for database connecion in every jsp page).



how is it possible??
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
Have a Utility java class that would perform database operations and call it from your JSP code.

This way your JSP code is clean and the database related logic is isolated in the utility class.


Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
abhijeet srivastava
Greenhorn

Joined: Aug 22, 2006
Posts: 26
Thanks Sunil!

Can you explain it in detail.

bye...
Rao Raghu
Ranch Hand

Joined: Jan 05, 2007
Posts: 100
It means that you have to write a javabean that has a method to establish database connection using JDBC. The method shall return a connection object. You have to call it with <jsp:useBean> action element in those JSPs where you want DB connections. Be sure to provide a code to close the connection after use.


RAGHU<br /> <br />"When the going gets tough, the tough get going"
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Not sure what you mean by explain in detail.

Bottom line...Have a class that will encapsulate all your database tasks such as connection, updates, select, etc. You will have several methods that will handle these functionality. Make the class available to your JSP, and call the methods at appropriate times from the JSP.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
And if you are still having issues, then refer to the site http://www.koders.com/java/fid38B38E125FCC3C5ABC6F37AC58E5F88C671544A1.aspx

Please check the license aspect of that site/code. I have given the reference just as a pointer to what the Utility class should contain. In designing lingo, this class is often referred as a BaseDAO, where DAO stands for Data Access Object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create common database connection in a application