• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating a unique id  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone recommend a way to generate a unique id?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gonna have to be more specific...
just want a random number generated, that isnt the same as one already made, or what?
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, basically a random number that can be assigned as an id, a number not already assigned.
In PHP there's a function called uniqueID() and I've been unable to unearth anything that approximates that in JAVA, so I was wondering if there are any preferred routines to do this.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at the Servlets section of The Cattle Drive and the suggested use of DataPool from The JavaRanch Common Package.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise, session tracking is pretty darn easy with servlets. Again, take a look at the Servlets section of the Cattle Drive.
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the heads - up.
Just been fiddling with this, I'm sure it's not the best way, but it's a start...
I'm thinking something along these lines:
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To clarify:
The id I want to generate is for a product_id field in a database, as I prefer not to rely on the auto_incrementing primary key.
I perhaps gave the impression I was looking for a session id...sorry.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here a link which claims to generate random numbers.
RandomX
Regards,
Marcel
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Simard:
The id I want to generate is for a product_id field in a database, as I prefer not to rely on the auto_incrementing primary key.


Why?
It seems to me as if the database was in the best position to guarantee the uniqueness of a key.
Random numbers are - by definition - not unique. You always had to check them against the database.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
The WEB SERVICES and JAX-RS Course
https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!