Java SQL Batch Execute executeBatchedUpdateDataType(Connection conn, String sql)

Here you can find the source of executeBatchedUpdateDataType(Connection conn, String sql)

Description

execute Batched Update Data Type

License

Open Source License

Declaration

public static void executeBatchedUpdateDataType(Connection conn,
            String sql) throws SQLException 

Method Source Code

//package com.java2s;
/*//from  w  w w  .ja va2s  . com
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

import java.math.BigDecimal;
import java.sql.*;

public class Main {
    public static void executeBatchedUpdateDataType(Connection conn,
            String sql) throws SQLException {

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

        byte b = 127;
        short s = 10000;
        long l = 1000000;
        float f = 3.6f;
        double d = 3.6;
        int in = 100;
        BigDecimal decimal = new BigDecimal(l);
        Date date = new Date(new java.util.Date().getTime());
        Time time = new Time(new java.util.Date().getTime());
        Timestamp timestramp = new Timestamp(new java.util.Date().getTime());

        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "10001");
            pstmt.setString(2, "varchar");
            pstmt.setBytes(3, "varbinary".getBytes());
            pstmt.setString(4, "C");
            pstmt.setBoolean(5, Boolean.FALSE);
            pstmt.setByte(6, b);
            pstmt.setByte(7, b);
            pstmt.setShort(8, s);
            pstmt.setShort(9, s);
            pstmt.setInt(10, in);
            pstmt.setInt(11, in);
            pstmt.setLong(12, l);
            pstmt.setLong(13, l);
            pstmt.setFloat(14, f);
            pstmt.setFloat(15, f);
            pstmt.setDouble(16, d);
            pstmt.setBigDecimal(17, decimal);
            pstmt.setBigDecimal(18, decimal);
            pstmt.setDate(19, date);
            pstmt.setTime(20, time);
            pstmt.setTimestamp(21, timestramp);
            pstmt.addBatch();
            pstmt.executeBatch();
            if (!conn.getAutoCommit()) {
                conn.commit();
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            close(pstmt);
        }

    }

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

        System.out.println("Update SQL: " + sql);
        byte b = 127;
        short s = 10000;
        long l = 1000000;
        float f = 3.6f;
        double d = 3.6;
        BigDecimal decimal = new BigDecimal(l);
        Date date = new Date(new java.util.Date().getTime());
        Time time = new Time(new java.util.Date().getTime());
        Timestamp timestramp = new Timestamp(new java.util.Date().getTime());
        timestramp.setNanos(1000);

        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < size; i++) {
                pstmt.setString(1, 100 + i + "");
                pstmt.setString(2, "varchar");
                pstmt.setBytes(3, "varbinary".getBytes());
                pstmt.setString(4, "C");
                pstmt.setBoolean(5, Boolean.FALSE);
                pstmt.setByte(6, b);
                pstmt.setByte(7, b);
                pstmt.setShort(8, s);
                pstmt.setShort(9, s);
                pstmt.setInt(10, i);
                pstmt.setInt(11, i);
                pstmt.setLong(12, l);
                pstmt.setLong(13, l);
                pstmt.setFloat(14, f);
                pstmt.setFloat(15, f);
                pstmt.setDouble(16, d);
                pstmt.setBigDecimal(17, decimal);
                pstmt.setBigDecimal(18, decimal);
                pstmt.setDate(19, date);
                pstmt.setTime(20, time);
                pstmt.setTimestamp(21, timestramp);
                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. executeBatchedUpdate(Connection conn, String sql, int size)
  6. executeBatches(PreparedStatement... statements)