import java.sql.*;
//import System.out.format;
//import org.postgresql.Driver;

public class Test {
    static final String DB_URL = "jdbc:postgresql://localhost/mydb";

    public static void main(String[] args) {
        Connection conn = null;
        // Open a connection
        try {
            conn = DriverManager.getConnection(DB_URL);
            conn.setAutoCommit(false);
			Integer increment = 0;

			// SELECT query
            PreparedStatement statement = conn.prepareStatement("select * from pg_class");
			statement.setFetchSize(100);
            ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				increment++;
			}
            conn.commit();
			statement.close();
			System.out.format("SELECT increment " + increment + "\n");

			//INSERT RETURNING
			//CREATE TABLE aa (a int);
			increment = 0;
            PreparedStatement insertStmt = conn.prepareStatement("insert into aa select oid from pg_class returning a");
			insertStmt.setFetchSize(100);
            ResultSet insertRs = insertStmt.executeQuery();
			while (insertRs.next()) {
				increment++;
			}
            conn.commit();
			insertStmt.close();
			System.out.format("INSERT RETURNING increment " + increment + "\n");

			//INSERT RETURNING
			//CREATE TABLE bb (a int);
			increment = 0;
            PreparedStatement withStmt = conn.prepareStatement("with insert_stmt AS (insert into bb select oid from pg_class returning a) SELECT r1.a, r2.a from insert_stmt as r1, aa as r2;");
			withStmt.setFetchSize(100);
            ResultSet withRs = withStmt.executeQuery();
			while (withRs.next()) {
				increment++;
			}
            conn.commit();
			insertStmt.close();
			System.out.format("INSERT WITH increment " + increment + "\n");

			increment = 0;
            PreparedStatement withStmt2 = conn.prepareStatement("with select_stmt AS (select oid from pg_class) SELECT r1.oid, r2.a from select_stmt as r1, aa as r2;");
			withStmt2.setFetchSize(100000);
            ResultSet withRs2 = withStmt2.executeQuery();
			while (withRs2.next()) {
				increment++;
			}
            conn.commit();
			insertStmt.close();
			System.out.format("SELECT WITH increment " + increment + "\n");
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ee) {
                ee.printStackTrace();
            }
        }
    }
}
