Package org.springframework.jdbc.support

Examples of org.springframework.jdbc.support.KeyHolder


      }
      else if (entity.getId() != null) {
        returnCode = executeStatementSet(stmtSet);
      }
      else {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        returnCode = jdbcTemplate.update(new PreparedStatementCreator() {
 
          public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
           
            LOG.info("Model INFO :" + stmtSet.getSql() + " param : " + Arrays.toString(stmtSet.getParams()));
            PreparedStatement ps = connection.prepareStatement(stmtSet.getSql(), new String[] { "id" });
            appendParam(ps, stmtSet.getParams());
            return ps;
          }
        }, keyHolder);
 
        long keyValue = keyHolder.getKey().longValue();
        entity.setId(keyValue);
      }
      LOG.info("INSERT returnCode : [" + returnCode + "]");
      entity.setTxnMode(TxnMode.UPDATE);
     
View Full Code Here


  private UpdateResult updateInternal(String procName, String sql, Object... args) throws DataAccessException {
    sql = checkAuthPlaceholder(sql);
    return new CallTemplate<UpdateResult>() {
      @Override
      UpdateResult doCall(String sql, MapSqlParameterSource params) {
        KeyHolder kh = new GeneratedKeyHolder();
        int rowsAffected = getJdbcTemplate().update(sql, params, kh);
        return new UpdateResult(kh, rowsAffected);
      }
    }.call(procName, sql, args);
  }
View Full Code Here

  ///// insert with start
  /**
   * 适用sqlserver,mysql 自动生成主键
   */
  protected void insertWithGeneratedKey(Object entity, String insertSql) {
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getNamedParameterJdbcTemplate().update(insertSql, new BeanPropertySqlParameterSource(entity) , keyHolder);
    setIdentifierProperty(entity, keyHolder.getKey().longValue());
  }
View Full Code Here

    return this.namedParameterJdbcTemplate.update(sql, param);
  }
  public Object executeSqlReturnKey(final String sql, Map<String, Object> param) {
    Object keyValue = null;
    try{
      KeyHolder keyHolder = new GeneratedKeyHolder();
      SqlParameterSource sqlp  = new MapSqlParameterSource(param);
      this.namedParameterJdbcTemplate.update(sql,sqlp, keyHolder);
      keyValue = keyHolder.getKey().longValue();
    }catch (Exception e) {
      keyValue = null;
    }
    return keyValue;
  }
View Full Code Here

    if (debug) {
      logger.info("sql:" + sql);
      logger.info("object:" + object);
    }
    SqlParameterSource namedParam = new BeanPropertySqlParameterSource(object);
    KeyHolder keyHolder = new GeneratedKeyHolder();
    int ret = getNameParameterJdbcTemplate().update(sql, namedParam, keyHolder);
    if (ret <= 0)
      return ret;
    try {
      if (keyHolder.getKey() == null)
        return ret;
      ret = keyHolder.getKey().intValue();
      return ret;
    } catch (Exception e) {
      return ret;
    }
  }
View Full Code Here

      throws DataAccessException {
   
    return new CallTemplate<UpdateResult>() {
      @Override
      UpdateResult doCall(String sql, MapSqlParameterSource params) {
        KeyHolder kh = new GeneratedKeyHolder();
        int rowsAffected = getJdbcTemplate().update(sql,
            params, kh);
        return new UpdateResult(kh, rowsAffected);
      }
    }.call(sql_, args_)
View Full Code Here

      jdbcTemplate.update(UPDATE_VENUE, venueData.getName(), venueData.getPostalAddress(), venueData.getLatitude(), venueData.getLongitude(), venueData.getLocationHint(), 1, venueId);
      logger.info("Updated venue (ID = " + venueId + ")");
      return eventId;
    } catch (IncorrectResultSizeDataAccessException e) {
      // insert if it doesn't exist
      KeyHolder keyHolder = new GeneratedKeyHolder();
      jdbcTemplate.update(new PreparedStatementCreatorFactory(INSERT_EVENT, new int[] {VARCHAR, VARCHAR, VARCHAR, TIMESTAMP, TIMESTAMP, VARCHAR, BIGINT})
          .newPreparedStatementCreator(new Object[] {eventData.getName(), eventData.getDescription(), eventData.getTimeZone(), eventData.getFirstDay(), eventData.getLastDay(), eventData.getAbbreviation(), eventData.getMemberGroupId()}), keyHolder);
      Number eventId = keyHolder.getKey();
      logger.info("Created event (ID = " + eventId + ")");
      jdbcTemplate.update(new PreparedStatementCreatorFactory(INSERT_VENUE, new int[] {VARCHAR, VARCHAR, DOUBLE, DOUBLE, VARCHAR, BIGINT})
          .newPreparedStatementCreator(new Object[] {venueData.getName(), venueData.getPostalAddress(), venueData.getLatitude(), venueData.getLongitude(), venueData.getLocationHint(), 1}), keyHolder);
      Number venueId = keyHolder.getKey();
      logger.info("Created vanue (ID = " + venueId + ")");
      jdbcTemplate.update(INSERT_EVENT_VENUE, eventId, venueId);
      jdbcTemplate.update(INSERT_EXTERNAL_EVENT, eventId, eventData.getSourceId(), eventData.getSource(), new Date());
      return eventId.longValue();
    }
View Full Code Here

      jdbcTemplate.update(UPDATE_LEADER, leaderData.getName(), leaderData.getBio(), leaderData.getPersonalUrl(), leaderData.getTwitterId(), leaderId);
      logger.info("Updated leader (ID = " + leaderId + ")");
      return leaderId;
    } catch (IncorrectResultSizeDataAccessException e) {
      // insert if it doesn't exist
      KeyHolder keyHolder = new GeneratedKeyHolder();
      jdbcTemplate.update(new PreparedStatementCreatorFactory(INSERT_LEADER, new int[] {VARCHAR, VARCHAR, VARCHAR, VARCHAR})
          .newPreparedStatementCreator(new Object[] {leaderData.getName(), leaderData.getBio(), leaderData.getPersonalUrl(), leaderData.getTwitterId()}), keyHolder);
      long leaderId = keyHolder.getKey().longValue();
      jdbcTemplate.update(INSERT_EXTERNAL_LEADER, leaderId, leaderData.getSourceId(), leaderData.getSource(), new Date());
      logger.info("Created leader (ID = " + leaderId + ")");
      return leaderId;
    }
  };
View Full Code Here

      jdbcTemplate.update(UPDATE_TIMESLOT, timeSlotData.getEventId(), timeSlotData.getLabel(), timeSlotData.getStartTime(), timeSlotData.getEndTime(), timeSlotId);
      logger.info("Updated timeslot (ID = " + timeSlotId + ")");
      return timeSlotId;
    } catch (IncorrectResultSizeDataAccessException e) {
      // insert if it doesn't exist
      KeyHolder keyHolder = new GeneratedKeyHolder();
      jdbcTemplate.update(new PreparedStatementCreatorFactory(INSERT_TIMESLOT, new int[] {BIGINT, VARCHAR, TIMESTAMP, TIMESTAMP})
        .newPreparedStatementCreator(new Object[] {timeSlotData.getEventId(), timeSlotData.getLabel(), timeSlotData.getStartTime(), timeSlotData.getEndTime()}), keyHolder);
      long timeSlotId = keyHolder.getKey().longValue();
      jdbcTemplate.update(INSERT_EXTERNAL_TIMESLOT, timeSlotId, timeSlotData.getSourceId(), timeSlotData.getSource(), new Date());
      logger.info("Created timeslot (ID = " + timeSlotId + ")");
      return timeSlotId;     
    }
  }
View Full Code Here

  /**
   * Method to execute the insert generating single key
   */
  private Number executeInsertAndReturnKeyInternal(final List<Object> values) {
    KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values);
    if (kh != null && kh.getKey() != null) {
      return kh.getKey();
    }
    else {
      throw new DataIntegrityViolationException(
          "Unable to retrieve the generated key for the insert: " + getInsertString());
    }
View Full Code Here

TOP

Related Classes of org.springframework.jdbc.support.KeyHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.