Java SQL Batch Execute executeBatchedUpdate(Connection conn, String sql, int size)

Here you can find the source of executeBatchedUpdate(Connection conn, String sql, int size)

Description

execute Batched Update

License

Open Source License

Declaration

public static void executeBatchedUpdate(Connection conn, String sql, int size) throws SQLException 

Method Source Code


//package com.java2s;
import java.sql.Connection;

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

import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    public static void executeBatchedUpdate(Connection conn, String sql, int size) throws SQLException {

        System.out.println("Update SQL: " + sql);

        PreparedStatement pstmt = null;
        try {/*  ww  w.  j  a  v  a  2  s  .  c o m*/
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < size; i++) {
                pstmt.setString(1, 110 + i + "");
                pstmt.setString(2, "Beijng");
                pstmt.setString(3, "Kylin Soong");
                pstmt.setString(4, "$8000.00");
                pstmt.setString(5, "Crystal Orange");
                pstmt.addBatch();
            }
            pstmt.executeBatch();
            if (!conn.getAutoCommit()) {
                conn.commit();
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            close(pstmt);
        }
    }

    public static void close(Connection conn) {

        if (null != conn) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {

            }
        }
    }

    public static void close(Statement stmt) {

        if (null != stmt) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {

            }
        }
    }

    public static void close(ResultSet rs, Statement stmt) {

        if (null != rs) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
            }
        }

        if (null != stmt) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {
            }
        }
    }
}

Related

  1. executeAsBatch(Connection con, List sqlList)
  2. executeBatch(File file, Connection con, String pluginName)
  3. executeBatch(PreparedStatement prepStmt)
  4. executeBatch(PreparedStatement ps)
  5. executeBatchedUpdateDataType(Connection conn, String sql)
  6. executeBatches(PreparedStatement... statements)