File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes DAO class design 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 » Databases » JDBC and Relational Databases
Bookmark "DAO class design" Watch "DAO class design" New topic

DAO class design

Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Hello ranch members,

I have the following scenario and would like suggestions on the best possible design for it.

TableA (id_A, blah1, blah2)
TableB (id_B, id_A)

I am using MySQL and id_A and id_B are primary keys for the respective tables. I am generating values for keys using AUTO_INCREMENT in MySQL.

I have a single DAO class and want to insert data into both these tables using it.
My problem is how would I insert a value for id_A in TableB.

The only way I could think of is was that insert values in TableA and the query the newly inserted record, use this id for TableB.

There are two problems with above approach one an extra select query and second since I am auto generating the key values I do not know the value of id_A for the newly created record.

Any suggestions are appreciated.


Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

You can insert A, using auto generated IDs,
then retrieve the auto generated IDs using Statement.getGeneratedKeys() ,
and use that in the insert for table B.

Regards, Jan

OCUP UML fundamental and ITIL foundation
Manhar Puri
Ranch Hand

Joined: Aug 23, 2005
Posts: 41
Thank you Jan for your response. That does provide a solution for my design. Do you have any ideas on improving the DAO design itself?
I agree. Here's the link:
subject: DAO class design
It's not a secret anymore!