me.icymint.libra.jdbc.query.Execute Maven / Gradle / Ivy
package me.icymint.libra.jdbc.query;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import me.icymint.libra.jdbc.JdbcAccessException;
import me.icymint.libra.jdbc.param.Parameter;
/**
* 批量执行方法。
*
* @author Daniel Yu
* @since 2013-3-14
*
* @param
* 执行参数。
*/
public class Execute
extends
AbstractExecute, PreparedStatement> {
/**
* 执行参数列表。
*/
private final Collection param;
/**
* 默认为批量执行。
*
* @param param
*/
public Execute(Collection
param) {
this.param = param;
}
@Override
protected PreparedStatement createStatement(Connection conn, String sql) {
try {
return conn.prepareStatement(sql);
} catch (SQLException e) {
throw new JdbcAccessException(e);
}
}
@Override
protected Void query(PreparedStatement u, String sql, Parameter
ps) {
try {
if (param != null) {
for (P p : param) {
u.clearParameters();
ps.handle(u, p);
u.addBatch();
}
u.executeBatch();
} else
throw new JdbcAccessException("无参数,故不执行!");
} catch (SQLException e) {
throw new JdbcAccessException("执行语句:" + sql, e);
}
return null;
}
}