Example usage for java.sql PreparedStatement addBatch

List of usage examples for java.sql PreparedStatement addBatch

Introduction

In this page you can find the example usage for java.sql PreparedStatement addBatch.

Prototype

void addBatch(String sql) throws SQLException;

Source Link

Document

Adds the given SQL command to the current list of commands for this Statement object.

Usage

From source file:com.thinkmore.framework.orm.hibernate.SimpleHibernateDao.java

/**
 * ?/*  w  w  w . j a va  2  s. co m*/
 * @param list sql?
 */
public void executeBatchByPrepare(String sql, final List<String> list) {
    Connection conn = null;
    PreparedStatement st = null;
    try {
        conn = SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection();
        conn.setAutoCommit(false); // ??
        st = conn.prepareStatement(sql);
        for (int i = 1, j = list.size(); i < j; i++) {
            Object objs = list.get(i - 1);
            if (objs instanceof List) {
                List<Object> values = (List<Object>) objs;
                for (int h = 1, k = values.size(); h <= k; k++) {
                    Object value = values.get(k - 1);
                    setParameters(st, k, value);
                }
            } else {
                setParameters(st, i, objs);
            }
            st.addBatch(sql);
            if (i % 240 == 0) {//?240?sql???
                st.executeBatch();
                conn.commit();
                st.clearBatch();
            } else if (i % j == 0) {//??
                st.executeBatch();
                conn.commit();
                st.clearBatch();
            }
        }
    } catch (Exception e) {
        try {
            conn.rollback();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    } finally {
        closeAll(st, conn);
    }
}

From source file:com.fortmoon.utils.CSVDBLoader.java

public void executeBatch() {
    PreparedStatement stmt = null;
    //Statement stmt = null;

    boolean first = true;
    StringBuffer insert = new StringBuffer("insert into " + this.tableName + " (");
    for (String col : this.columnNames) {
        if (first)
            insert.append(col);/*from www . j  av  a 2 s . c  o m*/
        else
            insert.append(", " + col);
        first = false;
    }
    insert.append(") values(");
    first = true;
    for (String col : this.columnNames) {
        if (first)
            insert.append("?");
        else
            insert.append(", ?");
        first = false;
    }
    insert.append(")");
    log.info("Insert statement: " + insert);

    try {
        con = DriverManager.getConnection(url, user, password);

        //stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        stmt = con.prepareStatement(insert.toString());

        con.setAutoCommit(false);
        TimeUnit SECONDS = TimeUnit.SECONDS;
        ArrayList<String> statements = null;
        boolean complete = false;
        while (!complete) {
            try {
                statements = (ArrayList<String>) queue.poll(10, SECONDS);
                if (statements == null) {
                    return;
                }
            } catch (InterruptedException e) {
                log.error("Poll timed out");
                e.printStackTrace();
                complete = true;
                return;
            }

            for (String statement : statements) {
                stmt.addBatch(statement);
            }
            //log.info("Starting execute.");
            int[] updateCounts = stmt.executeBatch();
            //log.info("Starting commit.");
            con.commit();
            stmt.clearBatch();
            log.info("Committed number of rows: " + updateCounts.length);
        }
    } catch (SQLException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        try {
            if (stmt != null)
                stmt.close();
            //if (pst != null)
            //   pst.close();
            if (con != null)
                con.close();
        } catch (SQLException ex) {
            log.error(ex.getMessage(), ex);
        }
        // statements.clear();
    }
    log.info("\nLoad complete. Goodbye.\n");
}