简体   繁体   中英

How shall I generate an id for each record in a database table?

I am following https://www.javatpoint.com/crud-in-servlet to create an CRUD application in servlets and mysql.

Each user inputs his information in a webpage and submit to the web server, which then invokes a servlet SaveServlet to save the information as a record in a database table. The database table however has an additional "id". SaveServlet.java doesn't create an id for each record. So I was wondering how to create an id for each record?

Thanks.

If you are using jpa/hibernate then your entity must be having the @Id annotation on the record id field (you dont need to set it, it will be done automatically based on you Id generation mechanism defined in the entity class and database schema).

if you are using plain jdbc for persisting records then you need to check the database how primary key is defined. for oracle you can your sequence.nextvalue to set the primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM