| Author |
Using a Bean for Retrieving Results Sets - Best Approach
|
Reggie McDougal
Ranch Hand
Joined: Sep 27, 2004
Posts: 69
|
|
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 ]
|
You can never drink too much
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26201
|
|
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.
|
[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
|
 |
Reggie McDougal
Ranch Hand
Joined: Sep 27, 2004
Posts: 69
|
|
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
Joined: Aug 15, 2004
Posts: 2874
|
|
|
do some reading on DAO, Data Access Object.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26201
|
|
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?
|
 |
 |
|
|
subject: Using a Bean for Retrieving Results Sets - Best Approach
|
|
|