This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java Objects and Database Records Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Objects and Database Records" Watch "Java Objects and Database Records" New topic

Java Objects and Database Records

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 212

Can anyone suggest suitable reading material to guide me in designing class's which specifically map to database tables and provide a "standard" approach to record manipulation. For example I find myself unsure as to the best practice for creating a new record in a table. Do I create a new instance of an object, passing the primary key to the constructor and allow the constructor to create a matching record OR do I create new object, set various fields and then call a method call .Create or .Insert

I'm happy with the mechanics of JDBC - I just need some advice as to best practice if indeed there is an agreed best way of doing this.

Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
Since you are using java objects and would like to create records in the database, I prefer using Object-Relational mapping tools like Hibernate. This is a suggestion because it makes things a lot easier where primary keys are automatically generated (object ids). However it is really slow when compared to jdbc.

"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I recently asked a similar question recently because I was working on a Swing application that needed to connect to a database. Perhaps the suggestion made there will help.

[ July 14, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Jacquie Barker
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
When designing an application with persistent storage, it's important to strive for model - data layer separation. That is, rather than embedding JDBC code into the constructor for a model object, e.g.

it's better to have a separate class(es) that provide database access and "serve" Person objects to the application:

I have an entire example of this in Chapter 15 of B.J.O. ... drop me an email ( and I'll elaborate.



Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
I agree. Here's the link:
subject: Java Objects and Database Records
It's not a secret anymore!