List of usage examples for java.sql PreparedStatement addBatch
void addBatch() throws SQLException;
PreparedStatement
object's batch of commands. From source file:Main.java
public static void execute_crop_stmt(PreparedStatement pstmt, int[] indexes, HashSet<Integer> set) throws SQLException { int cnt = 0;//from w ww . ja v a 2 s.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: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(); }/* ww w.j a va2 s .c o m*/ conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(true); close(prep); }
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. jav a2 s . com*/ 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:org.sonar.server.platform.db.migration.AutoDbMigration.java
/** * @param start included/* ww w . ja v a2s .c o m*/ * @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 {//from w ww .jav a 2 s .co m for (final DBRecord rec : records) { rec.writeFields(stmt); stmt.addBatch(); } stmt.executeBatch(); } finally { stmt.close(); } }
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 ava 2 s . co m*/ stmt.setString(2, pair.getValue()); stmt.addBatch(); } stmt.executeBatch(); }
From source file:gridool.util.jdbc.JDBCUtils.java
/** * Execute a batch of SQL INSERT, UPDATE, or DELETE queries. * //from ww w .j a va2 s. co 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; }
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;/* w ww . j ava2 s . 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:module.entities.NameFinder.DB.java
public static void InsertLexiconEntry(PreparedStatement statement, String lexiconType, String lemma, String entity, String lang) throws SQLException { try {//from w ww.ja v a2 s . c om statement.setString(1, lexiconType); statement.setString(2, lemma); statement.setString(3, entity); statement.setString(4, lang); statement.addBatch(); batchSize++; if (batchSize == 1000) { statement.executeBatch(); statement.clearBatch(); batchSize = 0; } } catch (SQLException ex) { System.err.println("Skipping:" + lemma + " " + entity + " " + ex.getMessage()); } }
From source file:net.firejack.platform.core.utils.db.DBUtils.java
private static void insertDataToTargetTable(TablesMapping mapping, Connection sourceConnection, Connection targetConnection) throws SQLException { Map<Column, Column> columnMapping = mapping.getColumnMapping(); if (columnMapping.isEmpty()) { logger.warn("No columns are detected - no data to insert."); } else {//from w ww . j a v a2 s .co m ResultSet rs = selectDataFromSource(sourceConnection, mapping); String insertQuery = populateInsertQuery(mapping); PreparedStatement insertStatement = targetConnection.prepareStatement(insertQuery); targetConnection.setAutoCommit(false); try { int currentStep = 1; while (rs.next()) { for (int i = 1; i <= columnMapping.size(); i++) { insertStatement.setObject(i, rs.getObject(i)); } insertStatement.addBatch(); if (++currentStep > DEFAULT_BATCH_SIZE) { insertStatement.executeBatch(); targetConnection.commit(); currentStep = 1; } } if (currentStep != 1) { insertStatement.executeBatch(); targetConnection.commit(); } } catch (SQLException e) { logger.error(e.getMessage(), e); targetConnection.rollback(); } finally { insertStatement.close(); rs.close(); } } }