File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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: 30138
    
150

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?
 
Don't get me started about those stupid light bulbs.
 
subject: Using a Bean for Retrieving Results Sets - Best Approach
 
Similar Threads
Singletons in servlets - threadsafe?
retrieving individual elements from an ArrayList
Class to handle connections
Nested Record sets and Joins
NameNotFoundException: MSAccessDS not bound