Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to generate a unique number

 
rob morkos
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create a unique number in java, because I need the unique Id before, so that I can insert in many tables within the database simultaneous without making any problems with my tables relations with each other. Is it possible and I would really appreciate any comments or code example. I would of liked to let my database generate it by making my field auto number, but the requirements was that I have the unique number before. Thanks in advance.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rob,
well, the generation of Unique ID depends a lot upon your application, you know. what kind of fields you have, what kind of other constraints you have. how you are going to use the Unique ID in your application etc....
but as far as i remember there isn't a direct way you can generate like GUID using some standard Java Class (please let me know if you don't know what GUID means) EXCEPT one RMI utility which uses IP address and an object reference unique across that IP address (across the JVM running on that machine).
look at,
java.rmi.server.ObjId
for details.
i am not sure if this is useful to you because this depends upon IP address and in RMI the server is usually having Unique IP (please post more questions on RMI at Remote Objects forum on Javaranch as i 'm not at all a wizard of RMI. i just know little bit) but in your case it might not be applicable you know.
just my 2 cents.
maulin
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getTime() method of the Date class returns the number of milliseconds since Jan 1st 1970. You could always just construct a date object and ask for it's time to get a unique number (it's a long). As long as you don't have so many users that they're asking for ID's faster than once a millisecond you should be fine.
 
Christopher Farnham
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a class that I wrote a while ago... I have used it for several projects.... use it at your own risk.
I have a GlobalUniqueObject interface so that you can use it via inheritance or composition.... if you see any problems with it, i'd appreciate it if you let me know....
Chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic