使用JdbcTemplate取得插入Oracle记录的主键值
使用JdbcTemplate取得插入Oracle记录的主键值 在使用spring的过程中经常遇到获取插入数据的主键值,在使用jdbc时我们可能已经会使用了,但是在spring的jdbcTemplate中也有相关的方法:
第一种方法:
public int getKey1(User user){
int i = 0;
final String name = user.getUserName();
final String password = user.getPassword();
final int age = user.getAge();
KeyHolder keyHolder = new GeneratedKeyHolder();
final String sql = "insert into tb_user(user_name,password,age) values(?,?,?)";
jdbcTemplate.update(new PreparedStatementCreator(){
public PreparedStatement createPreparedStatement(Connection conn)
throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql,new String[]{"user_id"});
ps.setString(1, name);
ps.setString(2, password);
ps.setInt(3, age);
return ps;
}
}, keyHolder);
i = keyHolder.getKey().intValue();
return i;
}
第二种方法:
public int getKey2(User user){
//这里withTableName("tb_user")面是 表名,usingGeneratedKeyColumns("user_id")表字段主键
SimpleJdbcInsert insertActor = new SimpleJdbcInsert(jdbcTemplate.getDataSource()).withTableName("tb_user").usingGeneratedKeyColumns("user_id");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("user_name", user.getUserName());
parameters.put("password", user.getPassword());
parameters.put("age", user.getAge());
Number newId = insertActor.executeAndReturnKey(parameters);
return newId.intValue();
}
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.text.ParseException;
- import org.springframework.jdbc.core.PreparedStatementCreator;
- import org.springframework.jdbc.support.GeneratedKeyHolder;
- import org.springframework.jdbc.support.KeyHolder;
- import com.kjlink.cms.form.model.LabelForm;
- import com.kjlink.cms.service.BaseService;
- public class ff extends BaseService {
- public int insertTable(LabelForm f) throws SQLException,ParseException
- {
- String content = f.getSiteId();
- final String sql = "insert into TAG_INFO(SITE_ID,NAME,CONTENT) values(?,?,'"+content+"')";
- KeyHolder keyHolder = new GeneratedKeyHolder();
- getJdbcTemplate().update(
- new PreparedStatementCreator() {
- public PreparedStatement createPreparedStatement(Connection con) throws SQLException
- {
- PreparedStatement ps = getJdbcTemplate().getDataSource()
- .getConnection().prepareStatement(sql,new String[]{ "SITE_ID" ,"NAME"});
- ps.setString(1, "站点号");
- ps.setString(2, "我的名字");
- return ps;
- }
- }, keyHolder);
- System.out.println("自动插入id============================" + keyHolder.getKey().intValue());
- return keyHolder.getKey().intValue();
- }
- }
评论
1 楼 pch272215690 2013-06-18 我比较喜欢用第三种方法:- GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
- SqlParameterSource paramSource = new BeanPropertySqlParameterSource(employe);
- final String sql = "INSERT INTO `employe` (`emp_name`, `sex`, `age`,`remark`) VALUES (:name, :sex, :age, :remark)";
- namedParameterJdbcTemplate.update(sql, paramSource, generatedKeyHolder,new String[]{"id_"});
- Number number = generatedKeyHolder.getKey();
- return number.longValue();
- GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
- SqlParameterSource paramSource = new BeanPropertySqlParameterSource(employe);
- final String sql = "INSERT INTO `employe` (`emp_name`, `sex`, `age`,`remark`) VALUES (:name, :sex, :age, :remark)";
- namedParameterJdbcTemplate.update(sql, paramSource, generatedKeyHolder,new String[]{"id_"});
- Number number = generatedKeyHolder.getKey();
- return number.longValue();
spring的版本是3.1.1,不知道老版本的namedParameterJdbcTemplate支持不。