• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

connection to database in a Bean or Servlet?

 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic