This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Database access via a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Database access via a static method" Watch "Database access via a static method" New topic
Author

Database access via a static method

Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Guys,

What's the general consensus on database access with a static method (or methods). Is this generally frowned upon?

For the sake of clarity, I'm thinking about some business logic which needs to read something from the database (say for instance whether a person is over a certain age limit).

Would it be bad to encapsulate this logic into a static helper method - and if so, why?

Thanks,

Chris

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

There is too little information there to say for sure if it is good or bad, but it does feel a little icky to me. The problem with static methods for this sort of thing is that they aren't very flexible - it makes it harder to reuse code without creating un-necessary dependencies, harder to mock an object for test, etc...

Since the database can be expected to change independently of the business logic (and the logic independently of the database) I feel it is generally better to isolate one from the other.

You might want or need to subclass the business object, and you might need to override or change the behavior of the way the data is taken: or you may want to generalize the interface to the business logic and how it gets the data it needs. This is all pretty simple to do until you have to think about static methods and shadowing and which version gets called. So for that reason I would rather the data-access be handled in an instance method rather than static (independent of whether it is in the same class as the business logic or not).


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database access via a static method
 
Similar Threads
Accessing variable from another method
Double-standard for inner class if it is non-static vs if it is static
Data Access Object Pattern
about DatemodelDbImpl and model !!
The question about LOCK,UNLOCK method