my dog learned polymorphism*
The moose likes JDBC and the fly likes Inserting prrimary key into other table as foreign key. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting prrimary key into other table as foreign key." Watch "Inserting prrimary key into other table as foreign key." New topic
Author

Inserting prrimary key into other table as foreign key.

Mohan Karthick
Ranch Hand

Joined: Apr 11, 2005
Posts: 199
Say I have one table EMP and other DEPT both are having auto generated primary key , and DEPT key keep EMP's promary key as a foreign key.

Now what is the best JDBC flow, if some onw wnat to insert emp info and dept infor together.
so flow is first it will insert EMP details and new row generate a primary key, I will pick up this primary by another JDBC call, and again inset it into DPET table with dept details, and another JDBC call. That means there are total three Databse call!!
Please advice any toher efficient flow.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30361
    
150

Mohan,
Three database calls isn't necessarily a problem. First write the code the clearest way and then time to see if there are any performance issues.

Depending on your database, you may only need two calls. Some databases (like Oracle) will return the generated key for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mohan Karthick
Ranch Hand

Joined: Apr 11, 2005
Posts: 199
All right .... Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting prrimary key into other table as foreign key.