• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using a Bean for Retrieving Results Sets - Best Approach

 
Reggie McDougal
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use a Bean for my DB Connections and results set by just passing in the SQL and returing a Result Set.

But I'm stunped here How can I close the connection?

I want to used this or a similar approach so I don't have to repeat the same code multiple times. If some one could give me a way I can achive this?



// Instantiating method



[ February 15, 2005: Message edited by: Reggie McDougal ]

[ February 15, 2005: Message edited by: Reggie McDougal ]
[ February 15, 2005: Message edited by: Reggie McDougal ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reggie,
You can't close the connection because it has a smaller scope than the result set. A good solution is to put that method in a superclass. You can provide hooks (methods to be implemented in the subclass) for setting the parameters and processing the resultset. Your idea is sound to separate out the db code.
 
Reggie McDougal
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting the DB_Bean as a super class to product solved the problem OK.

I have a couple of servlets that save form data and they cant extend DB_Bean.

What about setting up an interface to impliment the DB connection and returning the resultSet.

Is there more elegant solution than just sub classing to DB_Bean?
[ February 15, 2005: Message edited by: Reggie McDougal ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do some reading on DAO, Data Access Object.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reggie,
It's best if a servlet isn't going data access directly. You can use the DAO that Adeel suggested or at least a separate class.

I'm not sure how an interface to implement the DB connection would help you. Wouldn't you still have the problem with closing the connection?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic