0

I would like to ask where and how I can create an auto-generated item code (similar to primary key with auto-increment) for my form in JSP. As seen in this image >

link to JSP screen

I need to display the item codes on the screen even before inputting them into mySQL database (e.g. IGA01, IGA02, IGA03). Every time the user clicks the 'add row' button, a row is added and the item code should increment by 1. I'm using MVC to create my java web application...I'm somewhat lost where and how I should create this function (Should the generation function be done in JSP,servlet, or at the model classes?)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marielle
  • 81
  • 1
  • 2
  • 19
  • You could use something like this answer of mine [here](http://stackoverflow.com/a/38079598) by having different "sections" as I called them (I had to call them something). Make a call to the stored proc for the next incrementor by a section name. It uses safe row-level `intention locks` that are held momentarily. Now you have your number. Use that with your prefix (concat) and put that into a unique column. Left pad with zeros either in mysql (`lpad()`) or do that in java – Drew Sep 24 '16 at 09:40

1 Answers1

0

Create a Servlet and write these codes
Connection con = null;
ResultSet rs = null;
/* I assume that u have established connection /
Statement stmt = con.createStatement();
rs = stmt.excecuteQuery("SELECT count(
) FROM TABLE_NAME");
String id = "IGA" + count + "";
PreparedStatement ps = conn.prepareStatement("INSERT INTO TABLE_NAME(id) VALUES ('" + id + "');");
stmt.close();
con.close();
After use simple jquery code to display id on ur page.

  • Dont we feel the generation logic will fail in case I delete random records from db table – mhasan Sep 24 '16 at 10:13
  • For that case u can use SELECT cast(id as int) FROM TABLE_NAME ORDER BY id DESC LIMIT 1; –  Sep 24 '16 at 10:20