• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the best way to obtain a DataBase Connection ??

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to obtain a Connection in a Web application
In each servlet should I perform this operation

javax.naming.InitialContext ctx = new javax.naming.InitialContext();
ds = (javax.sql.DataSource) ctx.lookup("jdbc/ds1");


Or

shall I write a DataBaseUtility class which will return me a Connection instance .

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RaviNada Kiran wrote:shall I write a DataBaseUtility class which will return me a Connection instance .

That would be better. As your application grows, you'll want to keep all the database code in a separate package rather than in your servlet anyway.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lookup the DAO pattern how to do it correctly.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
RaviNada Kiran wrote:shall I write a DataBaseUtility class which will return me a Connection instance .

That would be better. As your application grows, you'll want to keep all the database code in a separate package rather than in your servlet anyway.



Thanks Jaenne , i will follow this way , as it will be nice to close the connections also.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic