使用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();
}
第一种方法:
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();
}
}