Get Last Inserted Id in a Table using Spring Framework and MySQL

int insert(final String a,final int b) {

KeyHolder keyHolder = new GeneratedKeyHolder();
this.getJdbcTemplate().update(new PreparedStatementCreator() {

public PreparedStatement createPreparedStatement(Connection conn)
throws SQLException {
// TODO Auto-generated method stub
PreparedStatement ps=conn.prepareStatement(“insert into table(a,b) values(?,?)”, new String [] {“id”});
ps.setString(1, a);
ps.setInt(2, b);

return ps;
}, keyHolder);

return keyHolder.getKey().intValue();


This example uses the Spring Framework’s classes though.

I have only tested it with MySQL (v5 to be specific), if you do try it with other databases; let me know if they work.