File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to generate a unique number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to generate a unique number" Watch "how to generate a unique number" New topic
Author

how to generate a unique number

rob morkos
Greenhorn

Joined: Nov 30, 2002
Posts: 1
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Jan 23, 2002
Posts: 144
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.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Christopher Farnham
Greenhorn

Joined: Sep 18, 2002
Posts: 12
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


Christopher Farnham<br />Boston, MA<p>"Perfect is the Enemy of Good"
 
wood burning stoves
 
subject: how to generate a unique number