I am implementing Employee Management System in Java. I want employee code to be auto generated on new employee registration and I should be in predefined format. I don't want to use auto incremented integer key as a employee ID. I am using MySQL Database and JDBC API to connect to MySQL. Please suggest me a solution.
Asked
Active
Viewed 1,590 times
-5
-
1DB ... autoincrement. – Stultuske Feb 20 '18 at 09:08
-
1Use a ``UUID``. – f1sh Feb 20 '18 at 09:08
-
2You could consider using a timestamp or randomly generated sequence as an id – sshashank124 Feb 20 '18 at 09:08
-
TimeStamp or time in ticks should work – G_S Feb 20 '18 at 09:09
-
1@sshashank124 neither of which would be guaranteed unique. – Stultuske Feb 20 '18 at 09:09
-
1"should be in predefined format" — _what_ format? – khelwood Feb 20 '18 at 09:13
-
Like company employee name Initials, Joining Year and Date of birth – Nilesh Deshmukh Feb 20 '18 at 09:16
-
1@NileshDeshmukh if that is the format you have to use, what are you asking about? but I hope you understand that none of this guarantees there won't be duplicates. – Stultuske Feb 20 '18 at 09:21
2 Answers
0
If the length of the ID does not matter just use java.util.UUID to generate a universally unique identifier.

RickP
- 62
- 6
-
-
1@NileshDeshmukh if you keep it short, you seriously minimize the number of possible users – Stultuske Feb 20 '18 at 09:12
-
-
@Stultuske if he uses a random sequence of numbers and letters then even a length of 5 would give him 36^5=60466176 possibilities. How likely is it that a person who has to ask how to generate a random string will have more than 60million users registered in his software? – OH GOD SPIDERS Feb 20 '18 at 09:15
-
@OHGODSPIDERS I've nowhere seen he intends to use 'numbers and letters'. You are assuming one thing, I'm assuming another. for all I know, all he wants to use are the numbers 1 through 5 – Stultuske Feb 20 '18 at 09:20
0
There can be many approaches to generate an employee Id.I am recommending following most commonly used methods.
1) Timestamp Based :- Use UUID from java.util.UUID to generate random ID's based on host and current time.
UUID uuid = UUID.randomUUID();
2) Custom Information Based
If you want an employeeId to be generated using Company Initials/Employee's Personal Information,
- Use employee's username and date of birth
Take a namespace identifier(can be your organisation's name)
With the hash of unique identifier and username of employee,
String source = namespace + username + dateofbirth; byte[] bytes = source.getBytes("UTF-8"); UUID uuid = UUID.nameUUIDFromBytes(bytes);
3) Use Company Initials : Lets say your company initials are XYZ.Use a random number generator function to generate any 5 or 6 digit number. Eg. XYZ47899

user2594
- 386
- 1
- 5
- 18