Java Code Examples for org.springframework.jdbc.core.JdbcOperations#batchUpdate()

The following examples show how to use org.springframework.jdbc.core.JdbcOperations#batchUpdate() . 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: NamedParameterBatchUpdateUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public static int[] executeBatchUpdateWithNamedParameters(
		final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {

	if (batchArgs.length == 0) {
		return new int[0];
	}

	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
	return jdbcOperations.batchUpdate(
			sqlToUse,
			new BatchPreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
					setStatementParameters(values, ps, columnTypes);
				}
				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 2
Source File: NamedParameterBatchUpdateUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
public static int[] executeBatchUpdateWithNamedParameters(
		final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {

	if (batchArgs.length == 0) {
		return new int[0];
	}

	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
	return jdbcOperations.batchUpdate(
			sqlToUse,
			new BatchPreparedStatementSetter() {
				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
					setStatementParameters(values, ps, columnTypes);
				}
				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 3
Source File: NamedParameterBatchUpdateUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql,
		final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
	if (batchArgs.length <= 0) {
		return new int[] {0};
	}
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
	return jdbcOperations.batchUpdate(
			sqlToUse,
			new BatchPreparedStatementSetter() {

				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
					setStatementParameters(values, ps, columnTypes);
				}

				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 4
Source File: NamedParameterBatchUpdateUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql,
		final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
	if (batchArgs.length <= 0) {
		return new int[] {0};
	}
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
	return jdbcOperations.batchUpdate(
			sqlToUse,
			new BatchPreparedStatementSetter() {

				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
					setStatementParameters(values, ps, columnTypes);
				}

				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 5
Source File: NamedParameterBatchUpdateUtils.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql,
		final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
	if (batchArgs.length <= 0) {
		return new int[] {0};
	}
	String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
	return jdbcOperations.batchUpdate(
			sqlToUse,
			new BatchPreparedStatementSetter() {

				@Override
				public void setValues(PreparedStatement ps, int i) throws SQLException {
					Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
					int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
					setStatementParameters(values, ps, columnTypes);
				}

				@Override
				public int getBatchSize() {
					return batchArgs.length;
				}
			});
}
 
Example 6
Source File: AbstractDeduplicateUsers.java    From find with MIT License 5 votes vote down vote up
private void updateSavedSearch(final JdbcOperations jdbcTemplate, final Long firstUserId, final List<Long> savedSearchIds) {
    final String updateSql = "UPDATE searches SET user_id=? WHERE search_id=?";
    jdbcTemplate.batchUpdate(updateSql, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(final PreparedStatement ps, final int i) throws SQLException {
            ps.setLong(1, firstUserId);
            ps.setLong(2, savedSearchIds.get(i));
        }

        @Override
        public int getBatchSize() {
            return savedSearchIds.size();
        }
    });
}
 
Example 7
Source File: AbstractDeduplicateUsers.java    From find with MIT License 5 votes vote down vote up
private void deleteDuplicateUsers(final JdbcOperations jdbcTemplate, final List<User> userEntities) {
    final String deleteSql = "DELETE FROM users WHERE user_id=?";
    jdbcTemplate.batchUpdate(deleteSql, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(final PreparedStatement ps, final int i) throws SQLException {
            ps.setLong(1, userEntities.get(i + 1).getUserId());
        }

        @Override
        public int getBatchSize() {
            // Don't delete the first user
            return userEntities.size() - 1;
        }
    });
}
 
Example 8
Source File: AbstractMigrateUsersToIncludeUsernames.java    From find with MIT License 5 votes vote down vote up
private void updateUsers(final JdbcOperations jdbcTemplate, final List<DeprecatedUser> users) {
    final String sql = getUpdateUserSql();

    jdbcTemplate.batchUpdate(sql, new UsersBatchPreparedStatementSetter(
            users,
            (ps, i) -> getBatchParameters(ps, users.get(i))
    ));
}
 
Example 9
Source File: AbstractMigrateUsersToIncludeUsernames.java    From find with MIT License 5 votes vote down vote up
private void deleteUsers(final JdbcOperations jdbcTemplate, final List<DeprecatedUser> users) {
    final String sql = "DELETE FROM users WHERE user_id=?";

    jdbcTemplate.batchUpdate(sql, new UsersBatchPreparedStatementSetter(
            users,
            (ps, i) -> ps.setLong(1, users.get(i).getUserId()))
    );
}