witek010
witek010

Reputation: 369

spring, jdbcTemplate, postgresql - last insert id

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

Answers (2)

Stefano
Stefano

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

Petar Minchev
Petar Minchev

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

Related Questions