Search code examples
javaspringpostgresqljdbctemplate

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 (?)

Solution

  • One easy option is to make the query like:

    "INSERT INTO jobs(jobs) VALUES(?) RETURNING row_id"
    

    And execute int id = getJdbcTemplate().queryForInt(sql).