• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

How to generate a unique ID?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I my java class, I am using JDBC to connect to the dtatabase.
Everytime I insert a recod, I wan to nenerate a unique id and add that id in the parimary_key column. How do I generate a unique id.
Thanks for your help!!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
why not you try, this ,
1. import java.util.Random
2. create a Random class object
3. and use nextInt() instance method to generate a integer number.
4. Concatenate it with primary key and before inserting into table, check whether this key already exist in table , if yes, repeat step 3 again.
ali
 
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use a sequence
------------------
http://www.x-nt.com
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
create a sequence in the database named someseq. Then in your program, add this
select someseq.nextval from dual
or
just 'someseq.nextval'(primary key field) in the insert statement
and add this value along with other column values.
Regards
Beksy
 
Bala Krishniah
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a Sequence, in the datadase.
Its name is SEQ_UUID
In my java code when I write the following code:
String uuid = SELECT SEQ_UUID.currval FROM dual;
Its is giving a error message saying: Expected ';'after the above line of code.
Also I want to append the sysdate to able generated 'uuid' in the MMDDYYYYHH24MISS formate. How do I alter the default value of the sysdate.
Thanks for your help in advance!!
 
SJ Adnams
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
INSERT INTO FOO (X,Y,Z) VALUES (SEQ_UUID.NEXTVAL, 10, 20);
Unfortunaty Oracle doesn't let you have NEXTVAL in the default clause of your create table.
------------------
http://www.x-nt.com
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use TO_CHAR(sysdate, 'MMDDYYYYHH24MISS')
 
reply
    Bookmark Topic Watch Topic
  • New Topic