hi,
actually i want to set a order id by automatically...
example:

oderId : GB0001

and it will increase automatically each time some one make order.

Dani AI

Generated

Short summary and a safe pattern to follow (for ):

As pointed toward database-managed IDs and showed a client-side string trick, the practical, production-ready approach is to keep an integer primary key managed atomically by the database and derive the human-facing code (GB0001) from that integer. That keeps uniqueness and concurrency safe, and keeps the numeric value available for joins/indexes.

A simple, reliable workflow (application side)

  1. Insert the order row letting the DB generate the numeric id.
  2. Retrieve the generated numeric id (JDBC getGeneratedKeys or SQL RETURNING).
  3. Format the display id by prefixing and zero-padding the number.

Example (JDBC + DecimalFormat):

PreparedStatement ps = conn.prepareStatement(
  "INSERT INTO orders (customer_id, total) VALUES (?, ?)",
  Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, 123);
ps.setBigDecimal(2, total);
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
  long id = rs.getLong(1);
  java.text.DecimalFormat df = new java.text.DecimalFormat("0000");
  String orderCode = "GB" + df.format(id);
}

A DB-only alternative
Use a generated/computed column so the DB stores the formatted code automatically (helps if you want the code queryable without extra app work). Add a UNIQUE constraint on that formatted column so duplicates can never appear.

Cautions and tips

  • Never generate the next id with SELECT MAX(id)+1 from the app — that breaks under concurrent inserts.
  • Decide how many digits you need (what happens at 9999?), and plan padding or longer formats.
  • Consider storing prefix separately if it might change (country codes, channels).
  • Keep the integer PK for joins and performance; keep the formatted code as display-only or as a generated/stored column.

This preserves data integrity while giving the readable GB0001 style output.

Recommended Answers

All 2 Replies

If I understand correctly (if I don't you should have provided a better explanation) you want to insert a new order in a database?
If yes there is an SQL command when you create a table where you set the Primary Key to be auto increment so when a new row is inserted the key will have a sequence number. Try to search how to create a "sequence" and how to get the next value at an SQL forum or at the internet.
If you want the String "GB" next to the number, then create the table like normal with a primary key, then declare the "sequence" and whenever you want to insert write a select that gets the next number and add to it the String "GB". Then use this value as order ID.

Of course everything is based on assuming that this was what you wanted to know. If I am wrong provide better explanation.
If I was right but you don't understand what I am saying it's my fault. Ask for better explanation at an SQL forum

Try to understand this code - It's string operation.

String old="GB0001"; // Assume that this value is come from Database
       int no=Integer.parseInt(old.substring(2,6));
       no++;
       String format="%04d";
       String newstr="GB" + String.format(format,no);
       System.out.println(newstr);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.