Ad

Is It Good Form In Java To Place Your DB Functions In Dedicated Class/methods?

In writing a relatively simple app, I was considering making a class that handled all the database interaction. I was going to construct all the prepared statements in the class. That way, any DB changes would (probably) only result in changes to that one class. (Also, it puts the DB user ID & password in one class.) For example, I was planning to write a class with a method to register the DB driver, another to make a connection, another to read, another to write, and yet another method to update.
Besides ease of maintaining the code, does this offer any other benefit? Maybe under a multithreaded context? Also, I was planning to passing as arguments to the query methods the variables to bind to the prepared statements. I was going to return the result set as an argument as well. Am I over thinking this? TIA.

Ad

Answer

I would say using ORM like hibernate is much better choice. It will reduce lot of boiler plate code. Plus much easier to read-write or write migrations (adding new fields or tables). I used similar approach for my college project which was purely java. I created a singleton class Database with all functions. Singleton class ensures only on copy of Database methods.

Ad
source: stackoverflow.com
Ad