• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

get the id (primary key) before I insert the query in sql  RSS feed

 
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
how I can do it if I want to store the id of the record in a value before i insert the record so I can use this id to insert different record in different table.

what I have now is insert statement to insert id (primary key of table),No,title,date ...... in table A, I want to save id before I insert or after so I can use it to insert id,A_id, points...…... in table B.


any idea about this issue
 
Bartender
Posts: 20125
103
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Wadhi!

JSF doesn't have any database capabilities of its own. That's handled by other frameworks such as raw SQL JDBC or JPA.

A primary key is meaningless unless you have a table row for it to the the key for. You can either generate key values in program logic or in many databases/frameworks, you can have the key automatically generated for you. There's usually a choice of value generation algorithms available.

The only really tricky thing here is that once you've generated a primary key value for one record in a table, you have to make sure that that same key value doesn't get assigned to some other record in the table. That's usually done by some sort of synchronization or transaction management.
 
wadha alketbi
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply

Acutely, I have my tables and I use sequence in order to generate the key, and in the java application in my dao class I have method to insert statement (insert record) to the DB, and in my query I use NEXTVAL to insert the id, but want I it now is before I insert, I want the to save the value of the id or even after I insert I want to save the value of the id in order to use it as a value in the second table because this id is a foreign key in that table.

I hope that you understand me.
 
Rancher
Posts: 3799
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this JDBC or JPA?
With JDBC you can tell the statement to return generated keys.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!