日期:2014-05-16  浏览次数:20491 次

spring jdbc 插入(Oracle)数据返回Id
spring jdbc 向oracle数据库中插入数据返回Id

final String sqlstr = "insert into tb_user(userId,userName,sex) values(seq_user_id.nextval,?,?)"
		KeyHolder keyHolder = new GeneratedKeyHolder();
		int result = 0;
		try {
			result = this.getJdbcTemplate().update(
					new PreparedStatementCreator() {
						public PreparedStatement createPreparedStatement(
								Connection conn) throws SQLException {
							PreparedStatement ps = null;
							ps = conn.prepareStatement(sqlstr,
									new String[] { "userId" });
							ps.setString(1, userName);
							ps.setString(2, sex);
							return ps;
						}
					}, keyHolder);
		} catch (Exception e) {
			result = 0;
		}

		if (result > 0)
			userId= keyHolder.getKeyList().get(0).toString();

new String[] { "userId" }); 这是向oracle数据库插入数据才用。
mysql中则不需要