Java Code Examples for org.springframework.jdbc.support.KeyHolder#getKey()

The following examples show how to use org.springframework.jdbc.support.KeyHolder#getKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExperimentDAOImpl.java    From gerbil with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public int createTask(String annotatorName, String datasetName, String experimentType, String matching,
        String experimentId) {
    MapSqlParameterSource params = createTaskParameters(annotatorName, datasetName, experimentType, matching);
    params.addValue("state", ExperimentDAO.TASK_STARTED_BUT_NOT_FINISHED_YET);
    java.util.Date today = new java.util.Date();
    params.addValue("lastChanged", new java.sql.Timestamp(today.getTime()));
    params.addValue("version", GerbilConfiguration.getGerbilVersion());
    KeyHolder keyHolder = new GeneratedKeyHolder();
    this.template.update(INSERT_TASK, params, keyHolder);
    Integer generatedKey = (Integer) keyHolder.getKey();
    if (experimentId != null) {
        connectToExperiment(experimentId, generatedKey);
    }
    return generatedKey;
}
 
Example 2
Source File: JdbcQueryVersionDAO.java    From ZenQuery with Apache License 2.0 6 votes vote down vote up
public Number insert(final QueryVersion queryVersion) {
    final String sql = "INSERT INTO query_versions (content, version, is_current_version, query_id) VALUES (?, ?, ?, ?)";

    jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();

    PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection)
                throws SQLException {
            PreparedStatement preparedStatement = connection.prepareStatement(sql, new String[] { "id" });

            preparedStatement.setString(1, queryVersion.getContent());
            preparedStatement.setInt(2, queryVersion.getVersion());
            preparedStatement.setBoolean(3, queryVersion.getIsCurrentVersion());
            preparedStatement.setInt(4, queryVersion.getQueryId());

            return preparedStatement;
        }
    };
    jdbcTemplate.update(
            preparedStatementCreator,
            keyHolder
    );

    return keyHolder.getKey();
}
 
Example 3
Source File: AbstractJdbcInsert.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Delegate method to execute the insert, generating a single key.
 */
private Number executeInsertAndReturnKeyInternal(final List<?> values) {
	KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values);
	if (kh.getKey() != null) {
		return kh.getKey();
	}
	else {
		throw new DataIntegrityViolationException(
				"Unable to retrieve the generated key for the insert: " + getInsertString());
	}
}
 
Example 4
Source File: AbstractJdbcInsert.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Delegate method to execute the insert, generating a single key.
 */
private Number executeInsertAndReturnKeyInternal(final List<?> values) {
	KeyHolder kh = executeInsertAndReturnKeyHolderInternal(values);
	if (kh.getKey() != null) {
		return kh.getKey();
	}
	else {
		throw new DataIntegrityViolationException(
				"Unable to retrieve the generated key for the insert: " + getInsertString());
	}
}
 
Example 5
Source File: AbstractJdbcInsert.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Delegate method to execute the insert, generating a single key.
 */
private Number executeInsertAndReturnKeyInternal(final List<?> 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());
	}
}
 
Example 6
Source File: AbstractJdbcInsert.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Delegate method to execute the insert, generating a single key.
 */
private Number executeInsertAndReturnKeyInternal(final List<?> 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());
	}
}
 
Example 7
Source File: GenericDao.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * @param entity
 * @param sql
 * @param params
 */
@SuppressWarnings("unchecked")
private ENTITY createAndFetchKey(ENTITY entity, final String sql, final List<Object> params) {

    KeyHolder keyHolder = new GeneratedKeyHolder();
    recordLog(sql);

    // 执行操作
    int rowCount = this.jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql);
            int index = 1;
            for (Object param : params) {
                ps.setObject(index++, param);
            }
            return ps;
        }
    }, keyHolder);

    // 如果插入成功则获取keyHolder中的key
    if (rowCount != 0 && keyHolder.getKey() != null) {
        Class<KEY> keyClass = orMapping.getKeyClass();
        if (keyClass.equals(Integer.class)) {
            entity.setId((KEY) Integer.valueOf(keyHolder.getKey().intValue()));
        } else if (keyClass.equals(Long.class)) {
            entity.setId((KEY) Long.valueOf(keyHolder.getKey().longValue()));
        }
    }

    return rowCount == 1 ? entity : null;
}
 
Example 8
Source File: AbstractJdbcInsert.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}
 
Example 9
Source File: JdbcQueryDAO.java    From ZenQuery with Apache License 2.0 5 votes vote down vote up
public Number insert(final Query query) {
    final String sql = "INSERT INTO queries (key, database_connection_id) VALUES (?, ?)";

    query.setKey(StringUtil.hashWithSha256(new Double(Math.random()).toString() + System.currentTimeMillis()));

    jdbcTemplate = new JdbcTemplate(dataSource);
    KeyHolder keyHolder = new GeneratedKeyHolder();

    PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection)
                throws SQLException {
            PreparedStatement preparedStatement = connection.prepareStatement(sql, new String[] { "id" });

            preparedStatement.setString(1, query.getKey());
            preparedStatement.setInt(2, query.getDatabaseConnectionId());

            return preparedStatement;
        }
    };
    jdbcTemplate.update(
            preparedStatementCreator,
            keyHolder
    );

    QueryVersion queryVersion = new QueryVersion();
    queryVersion.setQueryId(keyHolder.getKey().intValue());
    queryVersion.setContent(query.getContent());
    queryVersion.setIsCurrentVersion(true);
    queryVersion.setVersion(1);
    queryVersionDAO.insert(queryVersion);

    return keyHolder.getKey();
}
 
Example 10
Source File: GenericDao.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * @param entity
 * @param sql
 * @param params
 */
@SuppressWarnings("unchecked")
private ENTITY createAndFetchKey(ENTITY entity, final String sql, final List<Object> params) {

    KeyHolder keyHolder = new GeneratedKeyHolder();
    recordLog(sql);

    // 执行操作
    int rowCount = this.jdbcTemplate.update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql);
            int index = 1;
            for (Object param : params) {
                ps.setObject(index++, param);
            }
            return ps;
        }
    }, keyHolder);

    // 如果插入成功则获取keyHolder中的key
    if (rowCount != 0 && keyHolder.getKey() != null) {
        Class<KEY> keyClass = orMapping.getKeyClass();
        if (keyClass.equals(Integer.class)) {
            entity.setId((KEY) Integer.valueOf(keyHolder.getKey().intValue()));
        } else if (keyClass.equals(Long.class)) {
            entity.setId((KEY) Long.valueOf(keyHolder.getKey().longValue()));
        }
    }

    return rowCount == 1 ? entity : null;
}
 
Example 11
Source File: MessageRepositoryJDBCTemplate.java    From tutorials with MIT License 5 votes vote down vote up
public long insert(final String message) {
    
    KeyHolder keyHolder = new GeneratedKeyHolder();

    jdbcTemplate.update(connection -> {
        PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE_SQL);
        ps.setString(1, message);
        return ps;
    }, keyHolder);

    return (long) keyHolder.getKey();
}
 
Example 12
Source File: MessageRepositoryJDBCTemplate.java    From tutorials with MIT License 5 votes vote down vote up
public long insert(final String message) {
    
    KeyHolder keyHolder = new GeneratedKeyHolder();

    jdbcTemplate.update(connection -> {
        PreparedStatement ps = connection.prepareStatement(INSERT_MESSAGE_SQL);
        ps.setString(1, message);
        return ps;
    }, keyHolder);

    return (long) keyHolder.getKey();
}
 
Example 13
Source File: JdbcConferenceDaoImpl.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Override
public Long save(final Conference conference) {

	final String INSERT_SQL = "insert into conference (id, name, slug, description, started_date, ended_date, version) values (default, ?, ?, ?, ?, ?, 1) ";

	KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
	getJdbcTemplate().update(new PreparedStatementCreator() {

		@Override
		public PreparedStatement createPreparedStatement(
				Connection connection) throws SQLException {
			PreparedStatement ps = connection.prepareStatement(INSERT_SQL,
					new String[] { "id" });

			ps.setString(1, conference.getName());
			ps.setString(2, conference.getSlug());
			ps.setString(3, conference.getDescription());
			ps.setTimestamp(4, new java.sql.Timestamp(conference
					.getStartedDate().getTime()));
			ps.setTimestamp(5, new java.sql.Timestamp(conference
					.getEndedDate().getTime()));

			return ps;
		}
	}, generatedKeyHolder);

	return (Long) generatedKeyHolder.getKey();

}
 
Example 14
Source File: JdbcConferenceDaoImpl.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Override
public Long save(final Conference conference) {

	final String INSERT_SQL = "insert into conference (id, name, slug, description, started_date, ended_date, version) values (default, ?, ?, ?, ?, ?, 1) ";

	KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
	getJdbcTemplate().update(new PreparedStatementCreator() {

		@Override
		public PreparedStatement createPreparedStatement(
				Connection connection) throws SQLException {
			PreparedStatement ps = connection.prepareStatement(INSERT_SQL,
					new String[] { "id" });

			ps.setString(1, conference.getName());
			ps.setString(2, conference.getSlug());
			ps.setString(3, conference.getDescription());
			ps.setTimestamp(4, new java.sql.Timestamp(
					conference.getStartedDate().getTime()));
			ps.setTimestamp(5, new java.sql.Timestamp(
					conference.getEndedDate().getTime()));

			return ps;
		}
	}, generatedKeyHolder);

	return (Long) generatedKeyHolder.getKey();

}
 
Example 15
Source File: NamedParamJdbcTemplateConferenceDaoImpl.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Override
public Long save(final Conference conference) {

	final String INSERT_SQL = "insert into conference (id, name, slug, description, started_date, ended_date, version) values (default, ?, ?, ?, ?, ?, 1) ";

	KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(new PreparedStatementCreator() {

		@Override
		public PreparedStatement createPreparedStatement(
				Connection connection) throws SQLException {
			PreparedStatement ps = connection.prepareStatement(INSERT_SQL,
					new String[] { "id" });

			ps.setString(1, conference.getName());
			ps.setString(2, conference.getSlug());
			ps.setString(3, conference.getDescription());
			ps.setTimestamp(4, new java.sql.Timestamp(conference
					.getStartedDate().getTime()));
			ps.setTimestamp(5, new java.sql.Timestamp(conference
					.getEndedDate().getTime()));

			return ps;
		}
	}, generatedKeyHolder);

	return (Long) generatedKeyHolder.getKey();

}
 
Example 16
Source File: JdbcTemplateConferenceDaoImpl.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@Override
public Long save(final Conference conference) {

	final String INSERT_SQL = "insert into conference (id, name, slug, description, started_date, ended_date, version) values (default, ?, ?, ?, ?, ?, 1) ";

	KeyHolder generatedKeyHolder = new GeneratedKeyHolder();
	jdbcTemplate.update(new PreparedStatementCreator() {

		@Override
		public PreparedStatement createPreparedStatement(
				Connection connection) throws SQLException {
			PreparedStatement ps = connection.prepareStatement(INSERT_SQL,
					new String[] { "id" });

			ps.setString(1, conference.getName());
			ps.setString(2, conference.getSlug());
			ps.setString(3, conference.getDescription());
			ps.setTimestamp(4, new java.sql.Timestamp(conference
					.getStartedDate().getTime()));
			ps.setTimestamp(5, new java.sql.Timestamp(conference
					.getEndedDate().getTime()));

			return ps;
		}
	}, generatedKeyHolder);

	return (Long) generatedKeyHolder.getKey();

}