aspose file tools*
The moose likes JDBC and the fly likes Using a Bean for Retrieving Results Sets - Best Approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using a Bean for Retrieving Results Sets - Best Approach" Watch "Using a Bean for Retrieving Results Sets - Best Approach" New topic
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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31074
    
232

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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31074
    
232

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a Bean for Retrieving Results Sets - Best Approach