DBQueries { public static <K,A> int bulkAdd(Iterable<Row<K, A>> rows, final String tableName, final Connection connection){ final String sql = String.format("INSERT OR IGNORE INTO cqtbl_%s values(?, ?);", tableName); PreparedStatement statement = null; int totalRowsModified = 0; try { statement = connection.prepareStatement(sql); for (Row<K, A> row : rows) { statement.setObject(1, row.getObjectKey()); statement.setObject(2, row.getValue()); statement.addBatch(); } int[] rowsModified = statement.executeBatch(); for (int m : rowsModified) { ensureNotNegative(m); totalRowsModified += m; } return totalRowsModified; } catch (NullPointerException e) { boolean rolledBack = DBUtils.rollback(connection); NullPointerException npe = new NullPointerException("Unable to bulk add rows containing a null object to the index table: "+ tableName + ". Rolled back: " + rolledBack); npe.initCause(e); throw npe; } catch (Exception e){ boolean rolledBack = DBUtils.rollback(connection); throw new IllegalStateException("Unable to bulk add rows to the index table: "+ tableName + ". Rolled back: " + rolledBack, e); }finally { DBUtils.closeQuietly(statement); } }  static void createIndexTable(final String tableName, final Class<K> objectKeyClass, final Class<A> valueClass, final Connection connection); static boolean indexTableExists(final String tableName, final Connection connection); static void createIndexOnTable(final String tableName, final Connection connection); static void suspendSyncAndJournaling(final Connection connection); static void setSyncAndJournaling(final Connection connection, final SQLiteConfig.SynchronousMode pragmaSynchronous, final SQLiteConfig.JournalMode pragmaJournalMode); static SQLiteConfig.SynchronousMode getPragmaSynchronousOrNull(final Connection connection); static SQLiteConfig.JournalMode getPragmaJournalModeOrNull(final Connection connection); static void dropIndexOnTable(final String tableName, final Connection connection); static void dropIndexTable(final String tableName, final Connection connection); static void clearIndexTable(final String tableName, final Connection connection); static void compactDatabase(final Connection connection); static void expandDatabase(final Connection connection, long numBytes); static long getDatabaseSize(final Connection connection); static int bulkAdd(Iterable<Row<K, A>> rows, final String tableName, final Connection connection); static int bulkRemove(Iterable<K> objectKeys, final String tableName, final Connection connection); static int count(final Query<O> query, final String tableName, final Connection connection); static int countDistinct(final Query<O> query, final String tableName, final Connection connection); static java.sql.ResultSet search(final Query<O> query, final String tableName, final Connection connection); static java.sql.ResultSet getDistinctKeys(final Query<O> query, boolean descending, final String tableName, final Connection connection); static java.sql.ResultSet getKeysAndValues(final Query<O> query, boolean descending, final String tableName, final Connection connection); static int getCountOfDistinctKeys(final String tableName, final Connection connection); static java.sql.ResultSet getDistinctKeysAndCounts(boolean sortByKeyDescending, final String tableName, final Connection connection); static java.sql.ResultSet getAllIndexEntries(final String tableName, final Connection connection); static java.sql.ResultSet getIndexEntryByObjectKey(final K key , final String tableName, final Connection connection); static boolean contains(final K objectKey, final Query<O> query, final String tableName, final Connection connection);  }
@Test public void testBulkAdd() throws SQLException { Connection connection = null; try { ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true); createSchema(connectionManager); List<DBQueries.Row<Integer, String>> rowsToAdd = new ArrayList<DBQueries.Row<Integer, String>>(4); rowsToAdd.add(new DBQueries.Row<Integer, String>(1, "abs")); rowsToAdd.add(new DBQueries.Row<Integer, String>(1, "gps")); rowsToAdd.add(new DBQueries.Row<Integer, String>(2, "airbags")); rowsToAdd.add(new DBQueries.Row<Integer, String>(2, "abs")); connection = connectionManager.getConnection(null, noQueryOptions()); DBQueries.bulkAdd(rowsToAdd, NAME, connection); assertQueryResultSet("SELECT * FROM " + TABLE_NAME, rowsToAdd, connectionManager); }finally { DBUtils.closeQuietly(connection); } }