Spring provides
The following class shows how to retrieve the auto generated key after a new value is added to the table.
GeneratedKeyHolder class which can be used to retrieve the auto generated values.The following class shows how to retrieve the auto generated key after a new value is added to the table.
package com.javarewind.examples.spring;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
public class ExampleDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public long addNew(final String name) {
final PreparedStatementCreator psc = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(final Connection connection) throws SQLException {
final PreparedStatement ps = connection.prepareStatement("INSERT INTO `names` (`name`) VALUES (?)",
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, name);
return ps;
}
};
// The newly generated key will be saved in this object
KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(psc, holder);
long newNameId = holder.getKey().longValue();
return newNameId;
}
}
No comments:
Post a Comment