DeleteSelectedRows extends SwingWorkerPM { @Override protected Object doInBackground() throws SQLException { try(Connection connection = dataSource.getConnection()) { boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); int columnId = JDBCUtilities.getIntegerPrimaryKey(connection, tableName); if(columnId > 0) { String pkColumnName = JDBCUtilities.getFieldName(connection.getMetaData(), tableName, columnId); List<Long> deletionBatch = rowPkToDelete.getValueRanges(); ProgressMonitor pmBatch = getProgressMonitor().startTask(deletionBatch.size() / 2); try(PreparedStatement st = connection.prepareStatement( String.format("DELETE FROM %s WHERE %s >= ? AND %s <= ?", TableLocation.parse(tableName).toString(isH2),pkColumnName, pkColumnName))) { for (int idDelBatch = 0; idDelBatch < deletionBatch.size() - 1; idDelBatch += 2) { long startBatch = deletionBatch.get(idDelBatch); long endBatch = deletionBatch.get(idDelBatch + 1); st.setLong(1, startBatch); st.setLong(2, endBatch); st.execute(); pmBatch.endTask(); } } } } return null; } DeleteSelectedRows(SortedSet<Long> rowPkToDelete, String tableName, DataSource dataSource); static void deleteUsingRowSet(ReversibleRowSet reversibleRowSet, SortedSet<Long> rowPkToDelete);  }
@Test public void testDelete() throws SQLException { try(Connection connection = dataSource.getConnection(); Statement st = connection.createStatement()) { st.execute("DROP TABLE IF EXISTS \"MY TABLE\""); st.execute("CREATE TABLE \"MY TABLE\"(PK INTEGER PRIMARY KEY) AS SELECT X FROM SYSTEM_RANGE(1,50);"); LongUnion pkToDelete = new LongUnion(1, 25); DeleteSelectedRows deleteSelectedRows = new DeleteSelectedRows(pkToDelete, "MY TABLE", dataSource); deleteSelectedRows.doInBackground(); try(ResultSet rs = st.executeQuery("SELECT PK FROM \"MY TABLE\"")) { for(long expectedPk : new LongUnion(26,50)) { assertTrue(rs.next()); assertEquals(expectedPk, rs.getLong(1)); } assertFalse(rs.next()); } } }