Java DataSource batchExecute(DataSource ds, String sql, Object[]... args)

Here you can find the source of batchExecute(DataSource ds, String sql, Object[]... args)

Description

batch Execute

License

Apache License

Declaration

public static int[] batchExecute(DataSource ds, String sql, Object[]... args) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Arrays;

import javax.sql.DataSource;

public class Main {

    public static int[] batchExecute(DataSource ds, String sql, Object[]... args) {
        if (ds == null || sql == null || sql.trim().length() == 0)
            return null;

        int[] result = new int[] { 0 };

        Connection con = null;//  w  ww .  ja va  2s .  c  o m
        PreparedStatement pstmt = null;
        try {
            con = ds.getConnection();
            pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < args.length; ++i) {
                for (int j = 0; j < args[i].length; j++) {
                    pstmt.setObject(j + 1, args[i][j]);
                }
                pstmt.addBatch();
            }

            int[] rows = pstmt.executeBatch();
            if (rows != null && rows.length > 0) {
                result = new int[rows.length];
                if (sql.toUpperCase().contains("INSERT INTO")) {
                    ResultSet rs = pstmt.getGeneratedKeys();
                    int i = 0;
                    while (rs.next()) {
                        result[i] = rs.getInt(1);
                        i++;
                    }
                } else {
                    for (int i = 0; i < rows.length; i++) {
                        result[i] = rows[i];
                    }
                }
            }
            pstmt.close();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            close(null, pstmt, con);
            if (args != null && args.length > 0) {
                StringBuilder sb = new StringBuilder();
                for (Object[] _args : args) {
                    if (sb.length() > 0)
                        sb.append(",");

                    sb.append(Arrays.asList(_args));
                }
                System.out.println("---- " + sql + " [" + sb.toString() + "]");
            } else
                System.out.println("---- " + sql + " ----");
        }

        return result;
    }

    public static void close(ResultSet rs, PreparedStatement pstmt, Connection con) {
        try {
            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (con != null)
                con.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

Related

  1. assertNotNull(DataSource dataSource)
  2. checkTableExists(DataSource dataSource, String tableName)
  3. cleanDB(String ip, DataSource dataSource)
  4. countTable(DataSource dataSource, String tableName)
  5. createDataSource()