-5

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.

2 Answers2

0

If the length of the ID does not matter just use java.util.UUID to generate a universally unique identifier.

RickP
  • 62
  • 6
  • I want to keep length short – Nilesh Deshmukh Feb 20 '18 at 09:12
  • 1
    @NileshDeshmukh if you keep it short, you seriously minimize the number of possible users – Stultuske Feb 20 '18 at 09:12
  • Okay...Thanks @Stultuske – Nilesh Deshmukh Feb 20 '18 at 09:14
  • @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