3

I am currently developing a java application, which will communicate with a SQL server. Therefore I created the class Database which is responsible for open() and close().

I want to be able to add data to the database. In this example, the table is called job. Therefore I created a class Job. Besides these jobs, there will be (many) different things, which will be stored in the database. My question is: Where do I put my method addJob(Job j)? So far, I can think of three possible solutions:

  • Add the method addJob(Job j) to the Database class which provides abstraction for the rest of the program. The rest of the program does not have to worry how this is implemented. The downside of this is, that the Database will get very large eventually.

  • Create a seperate class JobHandler with the constructor public JobHandler(Database d) which accepts a Database. In this JobHandler I plan to provide the addJob(Job j). My problem is here, on which level should I interact with the Database since there are currently no public methods except open() and close(). Should I create a method which accepts pure sql statements? This does not look very good to me.

  • Make the class Database abstract and extend it so that I implement the missing methods here. Downside is, that I cannot pass the database around and use the same connection for multiple queries of different kinds.

What would one do to solve this problem? How should I structure my classes that they don't get too large and also not too much of the database class has to be exposed to the classes which implement the methods to deal with the data?

Edit Please note, that job is a random class, which should just serve as an example for the data which is stored in the database.

  • 5
    "What would one do to solve this problem?" There are roughly 9001 mature libraries that do exactly all this stuff. Use one of them. – whatsisname Sep 8 '16 at 0:36
  • 1
    Can you please give me an example for such a library. I would like to look into it. – Freddy Sep 8 '16 at 0:41
  • What do you mean by Job being just an example for data? – Tulains Córdova Sep 8 '16 at 1:00
  • I rephrased the sentence. – Freddy Sep 8 '16 at 1:02
  • Then find a better example, otherwise people willl think the problem is about database jobs (scheduled tasks), regardless of reading the explanatory edit. Also the question is too broad and/or not clear. – Tulains Córdova Sep 8 '16 at 1:09
4

The question is very confusing. What makes it more confusing is the fact that you decided to use Job and Jobhandler as examples which makes one think the question is about database jobs (scheduled tasks). But after reading the edit I guess you are trying to write a DAO and were talking about jobs as in "a position of employment".

Don't write a Database class. Just create a JDBC connection and pass it to the DAO.

You could either try to write a generic DAO or or write individual DAOs for each entity. DAOs will have SQL sentences inside it.

Another pattern is Active Record Pattern.

DAOs access databases, encapsulating SQL and allow you to populate the business objects. DAOs can also have methods that return lists of data.

Active Record is usually used to implementg business objects that manage write and read their data from the database themselves. This is considered too much since classes shouldn't maanage their own persistence. Active Records represent a single row of a table.

This question about refactoring an Active Record into DAO form will help you because it has a complete example of an object that accesses the databases and inserts or updates and its answers explain how to implement DAO instead.

  • 1
    You could mention ORM frameworks also. – COME FROM Sep 8 '16 at 9:47
  • 1
    @COMEFROM OP is a beginner. He must learn to swim before driving a jet ski. ORMs are incredibly complex. – Tulains Córdova Sep 8 '16 at 15:17
  • Thanks for your answer. I am referring to "just create a database connection". This would mean to create a connection in the main class with all the error handling etc.? Wouldn't this conflict with the single responsibility principle? – Freddy Sep 9 '16 at 4:18
  • The main class doesn't tipically comply with the SRP because it usually prepares the preconditions, reads configuration files etc., opens the main window in the case of GUI apps, or al least calls the methods that do that, its purpose is to be an entry point for the app. You can create a function that returns aconnection and call it from main or you can ask for a connection to a connection pool manager if you are using a web container that supports connection pools, etc. – Tulains Córdova Sep 9 '16 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.