aspose file tools*
The moose likes JDBC and the fly likes Generate Id. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Generate Id." Watch "Generate Id." New topic
Author

Generate Id.

pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Hi List[],

I am doing the project and need to generate the id for each record.I am using oracle as back end.Can body tell me how to generate unique id.


Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

In Oracle you can use sequences. Have a read of the chapter on sequences in the Oracle documentation.

The downside to using sequences is you introduce a database dependency. If this is a concern, have a google. for "primary key generation patterns" for some database neutral solutions.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Is any method in java by which we can generate long id's .If yes den
please help me.
Sachin Telang
Greenhorn

Joined: Nov 02, 2007
Posts: 19
Why you want java method/ you can create your own method not more than 2-5 lines. you can use any collection classes to keep in unique.
[ November 26, 2008: Message edited by: Sachin Telang ]

Thanks<br />Sachin Telang<br />IBM Websphere Portal Application Developer Certified, Exam -399.
Sun Certified Java Programmer 1.5
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by pankaj semwal:
Is any method in java by which we can generate long id's .If yes den
please help me.


Unique within the JVM? Yes, see java.lang.UUID. Unique within a database? No. The only thing that can guarentee this is the database itself.


Why you want java method/ you can create your own method not more than 2-5 lines. you can use any collection classes to keep in unique.

Again, this solution suffers from the problem that it can only be unique within your program, not unique within the database.
[ November 26, 2008: Message edited by: Paul Sturrock ]
pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Hi,
Can any body write the code for doing this.


Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by pankaj semwal:
Hi,
Can any body write the code for doing this.


Thanks


Please be aware that JavaRanch is NotACodeMill. What have you tried? Have you read the Oracle documentation on sequences? Have you googled for "primary key generation patterns"? Have you read the javaDocs for java.lang.UUID and considered the downsides of using this with a database?
[ November 26, 2008: Message edited by: Paul Sturrock ]
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

if you don't want to be database dependant (don't want to use oracle sequences) but need the id to be cross application, there is the old fasioned sequences_table solution
where you have a table and each row in the table contains:
the table that needs an id, and the next available id for that row.
you then simply selelct that value, add 1 and insert it back. (obviously locking the row before doing this)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate Id.