jdbc插入数据及插入数据返回他的id
1.插入数据(不返回他的id)
<1>这个也是插入数据,但是不返回当前行的id
<2>插入数据返回当前行的id
下面是代码
public Integer saveApplySubmit(final Prediction prediction) {
KeyHolder keyHolder = new GeneratedKeyHolder();
int id = 0;
try {
final String sql = "insert into t_prediction(school_id,grade_id,student_name,sex,family_name,id_number," +
"graduate_school,home_town,home_address) values (?,?,?,?,?,?,?,?,?)";
id = jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(
java.sql.Connection conn) throws SQLException {
int i = 0;
PreparedStatement ps = conn.prepareStatement(sql,
new String[] { "id" });
ps = conn.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, prediction.getSchoolId());
ps.setString(2, prediction.getGradeId());
ps.setString(3, prediction.getStudentName());
ps.setInt(4, prediction.getSex());
ps.setString(5, prediction.getFamilyName());
ps.setString(6, prediction.getIdNumber());
ps.setString(7, prediction.getGraduateschool());
ps.setString(8, prediction.getHomeTown());
ps.setString(9, prediction.getHomeAddress());
return ps;
}
}, keyHolder);
} catch (DataAccessException e) {
e.printStackTrace();
}
//logger.info(id + "=========000");
return keyHolder.getKey().intValue();
}