package com.vladmihalcea.book.high_performance_java_persistence.jdbc.batch;

import com.vladmihalcea.hibernate.masterclass.laboratory.util.AbstractPostgreSQLIntegrationTest;
import org.junit.Test;

import java.sql.Statement;

import static org.junit.Assert.assertEquals;

/**
 * <code>SimpleBatchStatementTest</code> - Simple Batch StatementTest
 *
 * @author Vlad Mihalcea
 */
public class SimpleBatchTest extends AbstractPostgreSQLIntegrationTest {

    private BatchEntityProvider batchEntityProvider = new BatchEntityProvider();

    @Override
    protected Class<?>[] entities() {
        return batchEntityProvider.entities();
    }

    @Test
    public void testStatement() {
        LOGGER.info("Test Statement batch insert");
        doInConnection(connection -> {
            try (Statement statement = connection.createStatement()) {

                statement.addBatch(
                    "insert into Post (title, version, id) " +
                    "values ('Post no. 1', 0, 1)");

                statement.addBatch(
                    "insert into PostComment (post_id, review, version, id) " +
                    "values (1, 'Post comment 1.1', 0, 1)");
                statement.addBatch(
                    "insert into PostComment (post_id, review, version, id) " +
                    "values (1, 'Post comment 1.2', 0, 2)");

                int[] updateCounts = statement.executeBatch();

                assertEquals(4, updateCounts.length);
            }
        });
    }
}

@@ -187,24 +187,59 @@
 unit.Test;%0A%0A
+import java.sql.PreparedStatement;%0A
 import java.
@@ -1479,9 +1479,9 @@
 als(
-4
+3
 , up
@@ -1531,11 +1531,893 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testPreparedStatement() %7B%0A        LOGGER.info(%22Test Statement batch insert%22);%0A        doInConnection(connection -%3E %7B%0A            PreparedStatement postStatement = connection.prepareStatement(%0A                    %22insert into Post (title, version, id) %22 +%0A                    %22values (?, ?, ?)%22);%0A%0A            postStatement.setString(1, String.format(%22Post no. %251$d%22, 1));%0A            postStatement.setInt(2, 0);%0A            postStatement.setLong(3, 1);%0A            postStatement.addBatch();%0A%0A            postStatement.setString(1, String.format(%22Post no. %251$d%22, 2));%0A            postStatement.setInt(2, 0);%0A            postStatement.setLong(3, 2);%0A            postStatement.addBatch();%0A%0A            int%5B%5D updateCounts = postStatement.executeBatch();%0A%0A            assertEquals(2, updateCounts.length);%0A%0A            postStatement.close();%0A        %7D);%0A    %7D
 %0A%7D%0A

Add PreparedStatament to Simple Batch Test