直接上代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Batch {
private String url;
private String user;
private String password;
public Batch(String url,String user,String password){
this.url = url;
this.user = user;
this.password = password;
}
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void insertBatch(String[] sqls) throws Exception{
try(Connection connection = DriverManager.getConnection(url, user, password))
{
//关闭自动提交,开启事务
connection.setAutoCommit(false);
//保存当前的自动提交模式
boolean autoCommit = connection.getAutoCommit();
//关闭自动提交
connection.setAutoCommit(false);
try(Statement stmtStatement = connection.createStatement())
{
//循环多次执行SQL语句