// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class OpenCloseFun {
    public static interface Connection extends AutoCloseable {
        public Statement createStatement();
    }
    public static interface Statement extends AutoCloseable {
        public ResultSet executeQuery(String sql);
    }
    public static interface ResultSet extends AutoCloseable {
        public boolean next();
    }
    public static class MyConnection implements Connection {
        private MyRealConnection real;
        private MyConnection(MyRealConnection real) {
            this.real = real;
        }
        public static MyConnection create() {
            return new MyConnection(new MyRealConnection());
        }
        public Statement createStatement() {
            return new MyStatement(real.createStatement());
        }
        public void close() throws Exception {
            real.close();
        }
        public String toString() {
            return real.toString();
        }
    }
    public static class MyStatement implements Statement {
        private MyRealStatement real;
        public MyStatement(MyRealStatement real) {
            this.real = real;
        }
        public ResultSet executeQuery(String sql) {
            return new MyResultSet(real.executeQuery(sql));
        }
        public void close() throws Exception {
            real.close();
        }
        public String toString() {
            return real.toString();
        }
    }
    public static class MyResultSet implements ResultSet {
        private MyRealResultSet real;
        public MyResultSet(MyRealResultSet real) {
            this.real = real;
        }
        public boolean next() {
            return real.next();
        }
        public void close() throws Exception {
            real.close();
        }
        public String toString() {
            return real.toString();
        }
    }
    public static class MyRealConnection implements Connection {
        private MyRealStatement stmt = new MyRealStatement(); // only one concurrent active Statement per Connection
        public MyRealStatement createStatement() {
            stmt.init();
            return stmt;
        }
        public void close() throws Exception {
            stmt.close();
        }
    }
    public static class MyRealStatement implements Statement {
        private MyRealResultSet rs = new MyRealResultSet(); // only one concurrent active ResultSet per Statement
        public void init() {
        }
        public MyRealResultSet executeQuery(String sql) {
            rs.init(sql);
            return rs;
        }
        public void close() throws Exception {
            rs.close();
        }
    }
    public static class MyRealResultSet implements ResultSet {
        public void init(String sql) {
        }
        public boolean next() {
            return true;
        }
        public void close() throws Exception {
        }
    }
    public static void main(String[] args) throws Exception {
        try(Connection con = MyConnection.create()) {
            try(Statement stmt = con.createStatement();
                ResultSet rs1 = stmt.executeQuery("something");
                ResultSet rs2 = stmt.executeQuery("something else")) {
                System.out.println(stmt);
                System.out.println(rs1);
                System.out.println(rs2);
            }
        }
    }
}
