Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

cascade insert: with sql or java?

Thread view: 
Andreas Bauer - 21 Apr 2006 21:13 GMT
Hi,

I'm a bit stuck. I have three tables over which I want to spread let's
user informations. I have a table where I insert username, pwd. Then I
have a table with extended user info where I want to insert forename,
surname etc. But for this insert I'd need the id from the first insert.
Do I do this with sql or should I do an select right after the insert to
get the id?

sql = "insert into......"
ps.executeUpdate(sql);
sql ="select id from table where username = dd"
rs = ps.executeQuery()
while(rs.next())
{   
int id = rs.getInt....
}

But this seems a bit circumstantial to me.

Any suggestions?

Regards,

Andi
chris brat - 21 Apr 2006 21:34 GMT
Have you considered using an external sequence ?

1.) Query the sequence for the id or use your own generated id and keep
in memory
2.) Populate the id as part of the "insert into..."
3.) Poplate the same id in the 2nd "insert into"

Chris
Andreas Bauer - 21 Apr 2006 21:40 GMT
chris brat schrieb:
> Have you considered using an external sequence ?
Yes. I thought about stored procedures or I wanted to lookup the feature
of CMP EntityBeans. But these techniques require some time of
orientation. So I thought of something "quicker"

> 1.) Query the sequence for the id
that's what I thought of

or use your own generated id and keep
Own generated? Before I do the insert, i. d. I create my own id and use
it for al 3 inserts?
chris brat - 21 Apr 2006 21:55 GMT
Exactly. ;-)

Stored procedures are usually the best option because they are 'closer'
to the database and can usually be optimised. If you were going to use
CMP beans you would still need to generate or lookup the key before you
inserted your rows - if I remember correctly it isn't done for you.

Your application could generate the ids, either a combination of
machine name, location, timestamp etc, combinations of name, surname,
idno, date of birth - thought these are not always possible or
appropriate.

For the application I currently work on I set up a "sequence" table,
which is just a table with a varchar(10) and an int column, because the
database we use doesnt support sequences itself.

I then wrote a Sequencer class with one method getNext(String
sequenceName) that returns an int value representing the next value
(The method does a query and an update on the row without the rest of
my application knowing the logic - I'll replace it if we change
database or I find a better solution).

I then use this id for numerous inserts into 2 different tables. I't
may not be pretty but it does work.
steve - 21 Apr 2006 22:12 GMT
> Hi,
>
[quoted text clipped - 21 lines]
>
> Andi

which database is it?

if it is oracle you can do a insert /update , with a returning clause

so it  would be " insert xxxxx into yyyy returning  id"

steve

Signature

NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth

spam@cognisense.com - 21 Apr 2006 23:39 GMT
With MySQL, I use:

SELECT LAST_INSERT_ID()

to get the unique key of the column which is defined as AUTO_INCREMENT

Works great.

Keith
www.cognisense.com
Andreas Bauer - 22 Apr 2006 08:14 GMT
> which database is it?

It's DB2.
Lee Fesperman - 22 Apr 2006 07:59 GMT
> Hi,
>
[quoted text clipped - 17 lines]
>
> Any suggestions?

JDBC 3 provides methods for retrieving keys generated by INSERT.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

chris brat - 22 Apr 2006 08:31 GMT
Hi Lee,

Is this the getGeneratedKeys() method in the Statement class?

C
Lee Fesperman - 23 Apr 2006 00:52 GMT
> Is this the getGeneratedKeys() method in the Statement class?

That's part of the puzzle, but you also need to request that generated keys be returned
by using extended forms of executeXXX() in java.sql.Statement or prepareStatement() in
java.sql.Connection.

Signature

Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)

bugbear - 24 Apr 2006 10:39 GMT
>>}
>>
[quoted text clipped - 3 lines]
>
> JDBC 3 provides methods for retrieving keys generated by INSERT.

But tragically (and annoyingly) it's not yet widely
supported/implemented.

  BugBear
ossareh@gmail.com - 24 Apr 2006 10:47 GMT
A question of form here: Do you need to seperate this information out?
As it all pertains to a user record surely then it should be in the
'user' table?

We currently have at least 5 tables which pertain to a user object,
which to be fair is just confusing. The tables were created to match
the user object which is split into a similar number of parts. This was
clearly a good idea when we designed our system but we're not realising
that this idea is not so great. Our next major release will involve
migrating all user "profile" data into one table.
ossareh@gmail.com - 24 Apr 2006 10:52 GMT
oh,

and if this is needed then feel free to copy the way that we do it :)

select id from user_id;
update user_id set id = id+1;

The select and update is in a synchronised method. Not a great solution
but it works for us.

note: that there is only one row in this table with a BIGINT.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.