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?
Asked
Active
Viewed 9,055 times
1
-
This is usually a job for timestamps. If you have a large number of users, maybe a user_id of some sort concatenated with the timestamp to make it more unique. – sorak Feb 16 '18 at 08:31
-
Timestamps are not so 100% sure to be unique, even when using a prefix. I would consider them a workaround. – dr0i Feb 16 '18 at 08:36
2 Answers
2
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).

Serge Ballesta
- 143,923
- 11
- 122
- 252
0
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.

Sumesh TG
- 2,557
- 2
- 15
- 29