I have to generate unique ID's for my application. After I used (UUID.randomUUID()).toString(), I get a code (thinking this is unique), that is very extended.

I don't know how unique it will likely be, whenever we generate codes with the aid of Java Timestamp or randomstring.

I have to generate unique codes that is only of 8-10 figures long (alpha-number). Ways to get so? I'm using MySQL database.

Is producing unique code on database side is the greatest way or are we able to generate such short (but unique) codes in Java?

Any suggestions with example code can be really useful.

Have you got any sort of limitation you have to consider? For example mix-application originality? Because otherwise, MySQL is very able to producing IDs alone, all that you should do is define an [cde] column and never specify it at place time (meaning, placing a autoincrement value for this) - that can make MySQL grow it using the next available ID, unique and needing no work of your stuff.

It will not be an alphanumerical string (which I am unsure should you specified like a requirement or restriction), but when whatever you should get is originality, it's ample. 7 alphanumeric figures aren't enough to ensure originality inside a at random-produced string, so you'd need to perform an place check up on the database.

Is producing unique code on database side is the greatest way or are we able to generate such short (but unique) codes in Java?

Databases are made to have the ability to generate unique IDs where needed. I doubt whatever you (or I) could code will be a 'better' variant of this.

I personally use NULL method from RandomStringUtils.randomAlphanumeric() to do this:

commons-lang

Should you using Maven, make sure that you have added import org.apache.commons.lang.RandomStringUtils; public static final int ID_LENGTH = 10; public String generateUniqueId() { return RandomStringUtils.randomAlphanumeric(ID_LENGTH); } to project's dependencies:

commons-lang

Is producing unique code on database side is the greatest way or are we able to generate such short (but unique) codes in java?

The choice is yours as well as your project. Is id-generation a part of business logic? If so and all sorts of logic writen on Java, so write it on Java. If any some a part of logic assigned to database, so generate id there (however in this situation you'll have strong dependency to specific database).