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.
If the length of the ID does not matter just use java.util.UUID to generate a universally unique identifier.
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
Related
I've been trying to use insert...returning in MySQL with the DSL-based table definition (I'm not using the code generation) and my returned record is always null. Based on reading, I need to specify the identify column in the table definition, but I have no idea how!
Record recordKey = create.insertInto(table("modulerecords"),
field("id"),
field("module_id"),
field("created_date"),
field("created_by"),
field("state"),
field("tag_id"),
field("start_time",Timestamp.class),
field("kill_time", Timestamp.class),
field("feed_guid")
)
.values(null, moduleId, currentTimestamp(),
userId, state, tagId,
new Timestamp(startTime),
new Timestamp(killTime), feedGuid)
.returning(field("id"))
.fetchOne();
The field "id" is auto_increment primary key in the database, but recordKey is always null.
As of jOOQ 3.14, this is possible by specifying the field's datatype as being an identity, which can be done using SQLDataType.INTEGER.identity(true).
So for example, if you had a table with an auto-generating integer id and a string name, you would call:
int id = DSL.using(connection, MYSQL_5_7)
.insertInto(
table("myTable"),
field("name", String.class))
.values("John Smith")
.returning(field("id", SQLDataType.INTEGER.identity(true)))
.fetchAny(field("id", Integer.class))
So for your example, you would do
Record recordKey = create.insertInto(table("modulerecords"),
field("id"),
field("module_id"),
field("created_date"),
field("created_by"),
field("state"),
field("tag_id"),
field("start_time",Timestamp.class),
field("kill_time", Timestamp.class),
field("feed_guid")
)
.values(null, moduleId, currentTimestamp(),
userId, state, tagId,
new Timestamp(startTime),
new Timestamp(killTime), feedGuid)
.returning(field("id", SQLDataType.INTEGER.identity(true)))
.fetchOne();
See this Github comment for more background.
It is highly recommended you use the code generator to provide all the meta information to the DSL API. You can, of course, not use the code generator and still use the internal APIs that the code generator would otherwise use. Instead of creaating your table and field references using the plain SQL API, you'd have to create a TableImpl subclass and override / implement all the relevant methods.
Or, you just use the code generator.
So I have simple java based server backend. I would like to create an empty customer and use an id that is generated in my application, not the id that is assigned by braintree. I tried to use this approach:
String testId = "12345678";
log.info("Create customer with id: " + testId);
CustomerRequest customerRequest = new CustomerRequest()
.customerId(testId)
.firstName("Martin")
.lastName("Mojko");
braintreeGateway.customer().create(customerRequest);
However, the setter customerId is ignored and braintree assigns different id to this customer.
It is there any way to use own ids?
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
You should use .id instead of .customerId to assign your generated ID to your customer. The request will look something like this:
CustomerRequest request = new CustomerRequest()
.id(testId)
.firstName("Martin")
.lastName("Mojko");
Result<Customer> result = gateway.customer().create(request);
You probably have to store a mapping between your IDs and theirs yourself.
This is very typical of 3rd party services everywhere. After all, the ID you supply to them may not match their ID generation scheme or may already be in use so they can't just use it themselves.
In my database, I have a uuid 67b26616-943e-49e0-a70c-8e9ddcba1baa. In my code, I want to query the database and want to pass the above uuid. How could I create a type UUID from a value? I saw the following function but I dont know which bits of my value goes where.
public UUID(long mostSigBits,
long leastSigBits)
You can create UUID this way:-
UUID uid = UUID.fromString("67b26616-943e-49e0-a70c-8e9ddcba1baa");
System.out.println("UUID value is: "+uid);
This question already has answers here:
Create a GUID in Java
(7 answers)
Closed 5 years ago.
I am less acquainted with front end and am just a beginner in back end. I am creating a webpage using servlets that reads data of a person from the database and displays it on the page in a form of a list. Each row of the list, consists of a button to contact. When clicked on the button, a message is sent to the person whose data has been selected. My question is- how do we create a unique id from the button click to generate a message id in the message table of the database awaiting a response from the selected person?
All decent databases provide a way to generate unique IDs. A common way is through the usage of sequences, but it can even be simpler, PostgreSQL for example provides the SERIAL and BIGSERIAL types that automatically create a new id for each inserted row.
Long story made short: if you only need a different id for each row, use the equivalent of SERIAL provided by your database, and if you need greater control directly use a SEQUENCE (or its equivalent).
Use UUID Generator
Starting with Java 5, the UUID class provides a simple means for generating unique ids. The identifiers generated by UUID are actually universally unique identifiers.
Example
import java.util.UUID;
public class GenerateUUID {
public static final void main(String... aArgs){
//generate random UUIDs
UUID idOne = UUID.randomUUID();
UUID idTwo = UUID.randomUUID();
log("UUID One: " + idOne);
log("UUID Two: " + idTwo);
}
private static void log(Object aObject){
System.out.println( String.valueOf(aObject) );
}
}
Example run:
>java -cp . GenerateUUID
UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00
UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889
Please refer : http://www.javapractices.com/topic/TopicAction.do?Id=56
I hope this will help.
I'm working with the low-level datastore API. I've created an entity like this:
Entity entity = new Entity("Error");
entity.setProperty("description", "foo");
In the datastore viewer, I ses this:
Key Write Ops ID/Name description
----------------------------------------------
ahN0c... 4 259 foo
So the ID/Name field will be generated for me automatically since I'm not supplying anything in the Entity constructor. It generates an "ID" instead of a "Name", which is a number rather than an opaque string (like the "Key" value).
Is there a way to have the datastore generate a random "Name" instead of an "ID" for the Entity's "ID/Name" field?
I ask because if I share this ID with third parties, they could start to figure out roughly how many Error instances I have in my system. I'd rather give them an opaque string for the lookup ID, similar to what's in the auto-generated "Key" field. But I don't see a way to do this.
Thanks
For a similar task I used UUID to create a random string.
String uuid = UUID.randomUUID().toString();
You can use com.google.appengine.api.datastore.KeyFactory, combining the answer from #Devolus, it would look like
final Key key = KeyFactory.createKey("Error", UUID.randomUUID().toString());
final Entity e = new Entity(key);
You could even pass around the String representation of your Entitie's key via KeyFactory.keyToString(key) , may be after an encrypting depending on your security needs.