Reputation: 369
I have:
public void addJobs(Jobs jobs) {
this.getJdbcTemplate().update(sqlAddJobs, new Object[] {jobs.getJobName()});
}
In Postgresql DBI have a table:
row_id | jobs
row_id is auto increment, how can I got last insert id?
My sql:
INSERT INTO jobs (jobs) VALUES (?)
Upvotes: 4
Views: 6716
Reputation: 1722
I say the following code:
this.getJdbcTemplate().queryForObject(sql, new Object[] {jobs.getJobName()},Integer.class);
in my case SPRING BOOT 2.2.4.RELEASE with JdbcTemplate I used the code above.
Upvotes: 3
Reputation: 47363
One easy option is to make the query like:
"INSERT INTO jobs(jobs) VALUES(?) RETURNING row_id"
And execute int id = getJdbcTemplate().queryForInt(sql)
.
Upvotes: 17