Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simpleJdbcTemplate. - insert and retrieve ID

I'm putting the data into database with simpleJdbcTemplate.

simpleJdbcTemplate.update("insert into TABLE values(default)");

I dont want to put any data because i dont need it for my unit test purpose.

How can i get the id from the inserted row? I can retriev the current sequence value but if somebody else will do a insert then i will be get a next sequence value.

Is there any way to use simpleJdbcTemplate to insert a row and get id? The update method retuns the number of inserted rows and i would like to have the id. Thank you for your help.

like image 869
Sebastian Avatar asked Feb 10 '10 08:02

Sebastian


1 Answers

Did you find the answer yet? If not, try to use SimpleJdbcInsert instead. For example:

SimpleJdbcInsert sji = new SimpleJdbcInsert(dataSource)
    .withTableName(TableName)
    .usingColumns(new String[]{your columns})
    .usingGeneratedKeyColumns(you auto-increment id colums);

then retrieve

sji.executeAndReturnKey(args).longValue();
like image 167
George Avatar answered Sep 21 '22 15:09

George