package ru.job4j.jdbc;

import java.sql.*;

public class Optimization {
    private int maxCount;
    private String url;
    private String userName;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String serverUrl) {
        this.url = "jdbc:postgresql://" + serverUrl;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMaxCount() {
        return maxCount;
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        return DriverManager.getConnection(url, userName, password);
    }

    public void insertRows(Connection connection) {
        try {
            Statement st = connection.createStatement();
            st.executeUpdate("DROP TABLE IF EXISTS TEST");
            st.executeUpdate("CREATE TABLE TEST (FIELD INT)");
            st.execute("CREATE OR REPLACE FUNCTION insertCount(n INTEGER) RETURNS VOID AS $$\n" +
                    "DECLARE c INTEGER := 1;\n" +
                    "BEGIN\n" +
                    "  LOOP\n" +
                    "    IF c > n THEN\n" +
                    "      EXIT;\n" +
                    "    END IF;\n" +
                    "    INSERT INTO test (field) VALUES (c);\n" +
                    "    c := c + 1;\n" +
                    "  END LOOP;\n" +
                    "END;\n" +
                    "$$ LANGUAGE plpgsql;");
            PreparedStatement statement = connection.prepareStatement("SELECT insertcount(?)");
            statement.setInt(1, maxCount);
            statement.execute();
        } catch (SQLException e) {
            for (Throwable t : e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        String myURL = "localhost:5432/sqlite";
        String myUser = "postgres";
        String myPassword = "shelby";
        int count = 1_000_000;
        Optimization opti = new Optimization();
        opti.setUrl(myURL);
        opti.setUserName(myUser);
        opti.setPassword(myPassword);
        opti.setMaxCount(count);
        try (Connection con = opti.getConnection()){
            opti.insertRows(con);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            for (Throwable t : e) {
                t.printStackTrace();
            }
        }
    }
}

@@ -975,16 +975,20 @@
 sertRows
+Func
 (Connect
@@ -1001,24 +1001,24 @@
 nnection) %7B%0A
-
         try 
@@ -2005,33 +2005,954 @@
                 
-e
+t.printStackTrace();%0A            %7D%0A        %7D%0A    %7D%0A%0A    public void insertRowsBatch(Connection con) %7B%0A        try %7B%0A            boolean autoCommit = con.getAutoCommit();%0A            con.setAutoCommit(false);%0A            Statement statement = con.createStatement();%0A            statement.executeUpdate(%22DROP TABLE IF EXISTS TEST%22);%0A            statement.executeUpdate(%22CREATE TABLE TEST (FIELD INT)%22);%0A            String command = %22INSERT INTO test (field) VALUES (%25s)%22;%0A            for (int i = 1; i %3C maxCount; i++) %7B%0A                statement.addBatch(String.format(command, i));%0A            %7D%0A            int%5B%5D counts = statement.executeBatch();%0A            if (counts.length != maxCount) %7B%0A                con.rollback();%0A            %7D else %7B%0A                con.commit();%0A            %7D%0A            con.setAutoCommit(autoCommit);%0A        %7D catch (SQLException e) %7B%0A            for (Throwable t : e) %7B%0A                t
 .printStackTrace
@@ -3446,16 +3446,17 @@
 ction())
+ 
 %7B%0A      
@@ -3457,24 +3457,26 @@
             
+//
 opti.insertR
@@ -3478,16 +3478,59 @@
 sertRows
+Func(con);%0A            opti.insertRowsBatch
 (con);%0A 

Add SQL and JDBC