Example usage for java.sql PreparedStatement executeBatch

List of usage examples for java.sql PreparedStatement executeBatch

Introduction

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

Prototype

int[] executeBatch() throws SQLException;

Source Link

Document

Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.

Usage

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the {@link PreparedStatement#addBatch() batches}, get the number of rows affected in
 * each batch, and close the statement./*from  w  w w  .  j  av a 2 s . c  o  m*/
 * 
 * @param stmt
 *            must already have batches added
 * @return null if the statement is null
 * @since 1.4.0
 */
public static int[] batch(PreparedStatement stmt) throws SQLException {
    int[] rows = null;
    if (stmt != null) {
        rows = stmt.executeBatch();
        stmt.close();
    }
    return rows;
}

From source file:Main.java

private static void saveAll(Connection conn, List<Person> people) throws SQLException {
    PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);");
    for (Person person : people) {
        prep.setString(1, person.getName());
        prep.setString(2, person.getOccupation());
        prep.addBatch();//from   www  .  j  av  a2 s .com
    }
    conn.setAutoCommit(false);
    prep.executeBatch();
    conn.setAutoCommit(true);
    close(prep);
}

From source file:Main.java

public static void execute_crop_stmt(PreparedStatement pstmt, int[] indexes, HashSet<Integer> set)
        throws SQLException {
    int cnt = 0;//from  ww  w. j ava  2s. c  o m
    for (int i = 0; i < indexes.length; i++) {
        if (!set.contains(indexes[i])) {
            pstmt.setInt(1, indexes[i]);
            pstmt.addBatch();
            cnt++;
            if (cnt > 5000) {
                pstmt.executeBatch();
                cnt = 0;
            }
        }
    }
    if (cnt > 0)
        pstmt.executeBatch();
}

From source file:org.sonar.server.platform.db.migration.AutoDbMigration.java

/**
 * @param start included/* ww  w  .j a v  a 2 s  .  c om*/
 * @param end excluded
 */
private static void batchExecute(long start, long end, PreparedStatement preparedStatement,
        Connection connection, Preparer preparer) throws SQLException {
    for (long i = start; i < end; i++) {
        preparer.prepare(preparedStatement, i);
        preparedStatement.addBatch();
        if (i % 250 == 0) {
            preparedStatement.executeBatch();
            connection.commit();
        }
    }
    preparedStatement.executeBatch();
    connection.commit();
}

From source file:gridool.db.DBInsertOperation.java

private static void executeInsertQuery(@Nonnull final Connection conn, @Nonnull final String sql,
        @Nonnull final DBRecord[] records) throws SQLException {
    final PreparedStatement stmt = conn.prepareStatement(sql);
    try {//  w w w.ja v a 2  s  . c o m
        for (final DBRecord rec : records) {
            rec.writeFields(stmt);
            stmt.addBatch();
        }
        stmt.executeBatch();
    } finally {
        stmt.close();
    }
}

From source file:net.big_oh.common.jdbc.JdbcProxyExerciser.java

private static void exercisePreparedBatchInsert(Connection con) throws SQLException {
    logger.info(StringUtils.center("exercise prepared batch insert", 100, "-"));

    PreparedStatement preparedStmnt = null;
    try {//from   ww  w .ja  v  a  2 s. c  om
        preparedStmnt = con.prepareStatement("INSERT INTO TEST_TABLE VALUES ( ? )");
        preparedStmnt.setString(1, "value4");
        preparedStmnt.addBatch();
        preparedStmnt.setString(1, "value5");
        preparedStmnt.addBatch();
        preparedStmnt.executeBatch();
    } finally {
        DbUtils.closeQuietly(preparedStmnt);
    }
}

From source file:module.entities.NameFinder.DB.java

public static void flushBatchLexiconEntry(PreparedStatement statement) throws SQLException {
    try {//  w  w  w .ja  v  a  2s . c  o  m
        statement.executeBatch();
        statement.clearBatch();
        batchSize = 0;
    } catch (SQLException ex) {
        System.err.println("Skipping:" + ex.getMessage());
    }
}

From source file:module.entities.NameFinder.DB.java

public static void saveClusterAnnotatedDocuments(TreeMap<Integer, String> docs) throws SQLException {
    String insertSQL = "INSERT INTO assignments_herc (annotator_id,text_id,json_out) VALUES (-1,?,?)";
    PreparedStatement stmt = connection.prepareStatement(insertSQL);
    for (Map.Entry<Integer, String> pair : docs.entrySet()) {
        stmt.setInt(1, pair.getKey());/*from  w w w.j av  a  2 s .  c  o  m*/
        stmt.setString(2, pair.getValue());
        stmt.addBatch();
    }
    stmt.executeBatch();
}

From source file:module.entities.NameFinder.DB.java

public static void InsertNewLemma2DB(String lemma, String category) throws SQLException {
    String selectSQL = "SELECT * FROM  WHERE  = ? ";
    PreparedStatement preparedStatement = connection.prepareStatement(selectSQL);
    preparedStatement.setString(1, lemma);
    preparedStatement.setString(2, category);
    ResultSet rs = preparedStatement.executeQuery();
    String insertSQL = "INSERT INTO  " + "(lemma_text,lemma_category) VALUES" + "(?,?)";
    PreparedStatement prepStatement = connection.prepareStatement(insertSQL);
    int id = -1;//from  www. j  av a  2s . co m
    if (rs.next()) {
        id = rs.getInt(1);
    } else {
        prepStatement.setString(1, lemma);
        prepStatement.setString(2, category);
        prepStatement.addBatch();
    }
    prepStatement.executeBatch();
    prepStatement.close();
}

From source file:gridool.util.jdbc.JDBCUtils.java

/**
 * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
 * //from   w w  w  . j a va 2  s. c o m
 * @param conn The Connection to use to run the query.  The caller is
 * responsible for closing this Connection.
 * @param sql The SQL to execute.
 * @param params An array of query replacement parameters.  Each row in
 * this array is one set of batch replacement values. 
 * @return The number of rows updated per statement.
 * @throws SQLException
 */
public static int[] batch(Connection conn, String sql, Object[][] params) throws SQLException {
    final boolean autoCommit = conn.getAutoCommit();
    if (autoCommit) {
        conn.setAutoCommit(false);
    }
    PreparedStatement stmt = null;
    int[] rows = null;
    try {
        stmt = conn.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            fillStatement(stmt, params[i]);
            stmt.addBatch();
        }
        verboseQuery(sql, (Object[]) params);
        rows = stmt.executeBatch();
    } catch (SQLException e) {
        rethrow(e, sql, (Object[]) params);
    } finally {
        close(stmt);
    }
    return rows;
}