aspose file tools*
The moose likes JDBC and the fly likes java.sql.Connection object, can it be a class var of a DAO? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.sql.Connection object, can it be a class var of a DAO?" Watch "java.sql.Connection object, can it be a class var of a DAO?" New topic
Author

java.sql.Connection object, can it be a class var of a DAO?

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi All. I am wondering if there is any reason NOT to do this. I have many DAO object that start like this :


Could I declare the conn, stmt, and rs variables in my BaseDAO class as protected class variables, and then in ProcedureDAO just do this ?


Then in BaseDAO I could have a method that closes all these objects, I could call it in the finally block of the getProcedure method and any other method of classes that extend BaseDAO.

I never see this in any example code, so I am wondering if there is a reason NOT to do it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

You are proposing to move the declarations of those variables to a wider scope. There's a design rule of thumb which says to declare variables in the narrowest possible scope.

But rules can be broken if there's a good enough reason. So it's up to you to provide a reason why you want to break that rule, not up to somebody else to provide a reason why you shouldn't. What's your reason?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.sql.Connection object, can it be a class var of a DAO?