Get up to 80 % extra points for free! More info:

Discussion: Generating Unique ID's in combinations of numbers and alphabets

Activities
Avatar
ATUAHENE OPPONG JONES:1/5/2017 10:03

please am stuck here. i want to generate unique Id's with letters like ROC200,ROC201 in this order.
please and to be able to save into the database and able to retrieve it.
i did one but because it was a string bringing it back from the data base was a problem.
thanks

Reply
1/5/2017 10:03
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/5/2017 10:35

And can't you just use numeric primary keys and then prefix them in your application with some product code? Generating unique string IDs is quite a problem, mainly if they should be incremented. Consider you happen to have multiple requests at the same time, you check what is the last ID, generate a new one but another thread of your application has just done the same so you end up with 2 same IDs.

If you really need GUIDs, I'd create a trigger and let the database itself to assign them. I tried to find some solution for you but it's apparently not a common approach so you'd have to come up with a solution by yourself.

There is the uuid() MySQL function which generates something like this:

aab5d5fd-70c1-11e5-a4fb-b026b977eb28

Maybe you could use this or go with prefixed numeric IDs.

Up Reply
1/5/2017 10:35
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
ATUAHENE OPPONG JONES:1/5/2017 12:45

i use the code in the attached file.
to give me ID's Starting from 10000,10001,1­0002etc
but I want it to be like ADM10000,ADM10001
thanks.

//CODE
int bid=9999
public void idincrement() {
String url = "jdbc:mysql://lo­calhost:3306/";
String dbName = "opma";
String driver = "com.mysql.jdbc­.Driver";
String userName = "root";
String password = "";

try {
Class.forName(dri­ver).newInstan­ce();
con = DriverManager­.getConnection(url + dbName, userName, password);
stmt = con.createSta­tement();
stmt = con.createSta­tement();
ResultSet res = stmt.executeQu­ery("select * from addstudent");
while (res.next()) {
bid = res.getInt(1);
}

} catch (ClassNotFoundEx­ception | InstantiationEx­ception | IllegalAccessEx­ception | SQLException e) {
JOptionPane.show­MessageDialog(nu­ll, "the error is" + e.getMessage());
//System.out.prin­tln(e.getMessa­ge());
}
bid = bid + 1;
sid.setText(In­teger.toStrin­g(bid));

}

Up Reply
1/5/2017 12:45
Technology For Life
Avatar
Replies to ATUAHENE OPPONG JONES
David Capka Hartinger:1/6/2017 13:33

Don't code this manually, just set the autoincrement value to 1000 to your database:

ALTER TABLE users AUTO_INCREMENT=1001;
Up Reply
1/6/2017 13:33
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
remote4me
Member
Avatar
remote4me:6/21/2019 14:46

Well, you do not need a database to generate Unique ID's

Just do this:

UUID.randomUUID()

One line.

And in case resulting string is too long: you can encode it with Base64 (but this is another story)

Edited 6/21/2019 14:48
 
Up Reply
6/21/2019 14:46
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

5 messages from 5 displayed.