0

I have to write an API which takes json { mobileno, password and emailId } & returns userId - which is autogenerated in table. My problem is, for this I have to hit the database 2 times, first for insert & then for getting user ID for the given mobileNo. Can it be done with 1 query, ie. how to get auto incremented value of userID column in return from the insert statement?

My table:

create table signup
(
   userid bigint auto_increment primary key,
   mobileNo bigint not null,
   password VARCHAR_CASESENSITIVE(255) not null,
   emailId varchar(320) not null
);

This is the query I am using for inserting the row:

jdbcTemplate.update("insert into SignUP (mobileNo, password, emailId) " + "values(?,  ?, ?)", new Object[{signup.getMobileNumber(), signup.getPassword(), signup.getEmail()})
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
neo
  • 41
  • 4
  • By definition, a save or insert query return the record id, so it is just one. – NiVeR Feb 05 '18 at 20:16
  • https://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/jdbc.html#jdbc-auto-genereted-keys – Andreas Feb 05 '18 at 20:19

0 Answers0