Reputation: 4620
i was doing a java program,as i was creating my second or third simple program of jdbc,i was entering the information about students in a table,in which there were four fields, the name,city & the roll no of the student which were added manually by the user through the java program(code),but i want that the fourth field id,(which is primary key,hence must be unique)must be generated automatically during the addition of the complete record,& it must be the value one more then the previous id's value present in the table......How could i do this?? i am not too good at sql queries..:)
Upvotes: 0
Views: 251
Reputation: 5399
It should be done via server (sql) side, try to read about auto increment for instance MySQL auto increment
And Yes, you have not set up auto increment from Java Side, for example if you have student table with some fields like id (auto_increment), name and age you should set just name and age from java side, id will be added from SQL side.
Upvotes: 1
Reputation: 31
which IDE are you using????
CREATE TABLE "student"
(
"stud_id" INT not null primary key
GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
"NAME" VARCHAR(50),
"CITY" VARCHAR(50),
"ROLL NO." INTEGER
);
Upvotes: 2