[java] Proper usage of databases?

Started by
2 comments, last by Jonny_S 16 years, 2 months ago
I'm working on a project which requires regular database usage, I'm using the DB4O database engine but I'm not having to get too involved with it, just storage of fairly simple objects. However this is my first time really using databases and I was wondering what is the best method for interacting with my database? So far I've come up with two options: either create a database handler class, or have each class handle its own db interaction. I'm not the best at explaining things so I'll try to show an example of what I mean. Say for example I'm trying to store cars in a database, so I have a class called Car. I could have another class called DatabaseIO which has methods such as addCar(), deleteCar(), findCar() etc, or I could have static methods within the Car class to do this. I've been swaying between the two options, I think having a DatabaseIO class would probably be better, it keeps everything separate and seems a more OO way of doing things, but then again I'm pretty new to OO design. Cheers for the advice, sorry for the waffle!
Advertisement
If you read through DB4O documentation, you'll see that it's an object database. This simply means that you tell it which objects to persist.

More specifically, the Chapter 2(PDF) of tutorial.
Use the 'DatabaseIO' class in preference to static methods. If it starts getting bulky, split it up into multiple objects (see DAO pattern).


Thanks for the replies, I went ahead with the single class so I'm glad to see I went with the right (or better) choice.

This topic is closed to new replies.

Advertisement