aspose file tools*
The moose likes JDBC and the fly likes What is the best way to obtain a DataBase Connection ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the best way to obtain a DataBase Connection ??" Watch "What is the best way to obtain a DataBase Connection ??" New topic
Author

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

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
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 .


If you want something you never had do something which you had never done
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29253
    
139

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.


[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
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Lookup the DAO pattern how to do it correctly.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the best way to obtain a DataBase Connection ??
 
Similar Threads
J2ee application and JNDI in a Servlet
Confused with Best pratices of JUnit
service method - when does it end
getEnvironment()
Connection Object pointing to Null