Java SQL Batch Execute executeAsBatch(Connection con, List sqlList)

Here you can find the source of executeAsBatch(Connection con, List sqlList)

Description

execute As Batch

License

Open Source License

Declaration

public static int[] executeAsBatch(Connection con, List<String> sqlList) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.*;
import java.util.*;

public class Main {
    public static int[] executeAsBatch(Connection con, List<String> sqlList) throws SQLException {
        return executeAsBatch(con, sqlList.toArray(new String[] {}));
    }/*from  w  w  w .j a va2 s .c  om*/

    public static int[] executeAsBatch(Connection con, String[] sqlArray) throws SQLException {
        Statement stmt = null;
        try {
            stmt = con.createStatement();
            for (String sql : sqlArray) {
                stmt.addBatch(sql);
            }
            return stmt.executeBatch();
        } finally {
            if (null != stmt) {
                stmt.close();
            }
        }
    }

    public static int[] executeAsBatch(Connection con, String sql, Object[][] params) throws SQLException {
        PreparedStatement preStmt = null;
        try {
            preStmt = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                Object[] rowParams = params[i];
                for (int k = 0; k < rowParams.length; k++) {
                    Object obj = rowParams[k];
                    preStmt.setObject(k + 1, obj);
                }
                preStmt.addBatch();
            }
            return preStmt.executeBatch();
        } finally {
            if (null != preStmt) {
                preStmt.close();
            }
        }
    }
}

Related

  1. executeBatch(File file, Connection con, String pluginName)
  2. executeBatch(PreparedStatement prepStmt)
  3. executeBatch(PreparedStatement ps)
  4. executeBatchedUpdate(Connection conn, String sql, int size)