Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create common database connection in a application

 
abhijeet srivastava
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
abhijeet srivastava
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sunil!

Can you explain it in detail.

bye...
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic