Two Laptop Bag
The moose likes JDBC and Relational Databases and the fly likes Class to handle connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Class to handle connections" Watch "Class to handle connections" New topic

Class to handle connections

Reggie McDougal
Ranch Hand

Joined: Sep 27, 2004
Posts: 69
A question, is it OK to pass my SQL into a DB_Bean that gets the connection
and passes back the resultSet, what about closing the connection, will this happen automatically when the method returns or is this just a bad practice

My method is like so that calls the DB_Bean


You can never drink too much
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
if you are not using the DBCP, then you should get the connection when required and close it after use. Moreover you should take care of thread-safety.
[ December 13, 2004: Message edited by: Adeel Ansari ]
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
No. The connection is not closed automatically. The resultset returned from the method, still maintains a connection to the database.
The RowSet might be an alternative.

Or you have to populate the values from resultset into your own objects and return them instead of returning the resultset (and close the connection in the getRecords() method.

Quaerendo Invenietis
I agree. Here's the link:
subject: Class to handle connections
It's not a secret anymore!