Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.db.sql;
import org.dromara.hutool.core.collection.iter.ArrayIter;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.stream.StreamUtil;
import org.dromara.hutool.db.DbException;
import org.dromara.hutool.db.config.DbConfig;
import org.dromara.hutool.db.handler.ResultSetUtil;
import org.dromara.hutool.db.handler.RsHandler;
import java.sql.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Statement和PreparedStatement工具类
*
* @author looly
* @since 4.0.10
*/
public class StatementUtil {
/**
* 创建{@link PreparedStatement}
*
* @param returnGeneratedKey 当为insert语句时,是否返回主键
* @param config 数据库配置
* @param conn 数据库连接
* @param sql SQL语句,使用"?"做为占位符
* @param params "?"对应参数列表或者Map表示命名参数
* @return {@link PreparedStatement}
* @since 5.8.19
*/
public static PreparedStatement prepareStatement(final boolean returnGeneratedKey, final DbConfig config, final Connection conn, final String sql, final Object... params) {
return StatementBuilder.of()
.setConnection(conn)
.setReturnGeneratedKey(returnGeneratedKey)
.setSqlFilter(Opt.ofNullable(config).map(DbConfig::getSqlFilters).getOrNull())
.setSql(sql)
.setParams(params)
.build();
}
/**
* 创建批量操作的{@link PreparedStatement}
*
* @param config 数据库配置
* @param conn 数据库连接
* @param sql SQL语句,使用"?"做为占位符
* @param paramsBatch "?"对应参数批次列表
* @return {@link PreparedStatement}
* @since 4.1.13
*/
public static PreparedStatement prepareStatementForBatch(final DbConfig config, final Connection conn, final String sql, final Object[]... paramsBatch) {
return prepareStatementForBatch(config, conn, sql, new ArrayIter<>(paramsBatch));
}
/**
* 创建批量操作的{@link PreparedStatement}
*
* @param config 数据库配置
* @param conn 数据库连接
* @param sql SQL语句,使用"?"做为占位符
* @param paramsBatch "?"对应参数批次列表
* @return {@link PreparedStatement}
* @since 4.1.13
*/
public static PreparedStatement prepareStatementForBatch(final DbConfig config, final Connection conn, final String sql,
final Iterable