File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes connection to database in a Bean or Servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "connection to database in a Bean or Servlet?" Watch "connection to database in a Bean or Servlet?" New topic
Author

connection to database in a Bean or Servlet?

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi, I was reading some of the latest posts and I found this one:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=007630
here, it uses a bean to make a connection to a database. Then you have a servlet that receives some parameters and perform a particular task using the bean. My question is:
What is the best practice, to have all the process of connecting to a database in a servlet or JavaBean..?
thanks


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
For one thing, use a connection pool instead of trying to manage db connections yourself. Your servlet must be designed so that connections are returned to the pool no matter what happens.
Bill
George Carlson
Greenhorn

Joined: Aug 24, 2000
Posts: 15
The best implementation of this is to have one class that both your servlets and jsp's have access to for each table in your database with variables corresponding to each field in your database. Each time you need access to the database, use only this class. That way, you can synchronize on that class and you'll never have to worry about having bad data, especially when adding new data or updating. Just doing selects, it isn't as big of a deal, but it is still much more managable having just one class that both servlets and the jsp has access to.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
sortsol,
Your display name does not conform to the Javaranch naming policy. Please read this policy and change your display name if you wish to continue posting here. Thanks.


Matthew Phillips
 
GeeCON Prague 2014
 
subject: connection to database in a Bean or Servlet?