// 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) 
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DB2ListVar {
    public static class TypeUnsafeBean {
        public Object get(String fld) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
            return new PropertyDescriptor(fld, this.getClass()).getReadMethod().invoke(this);
        }
    }
    public static class T1 extends TypeUnsafeBean {
        private int f1;
        private String f2;
        public T1(int f1, String f2) {
            this.f1 = f1;
            this.f2 = f2;
        }
        public void setF1(int f1) {
            this.f1 = f1;
        }
        public int getF1() {
            return f1;
        }
        public String getF2() {
            return f2;
        }
        public void setF2(String f2) {
            this.f2 = f2;
        }
    }
    private static List<T1> loadData() throws SQLException {
        List<T1> res = new ArrayList<>();
        try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "root", "")) {
            try(Statement stmt = con.createStatement()) {
                try(ResultSet rs = stmt.executeQuery("SELECT f1,f2 FROM t1")) {
                    while(rs.next()) {
                        res.add(new T1(rs.getInt("f1"), rs.getString("f2")));
                    }
                }
            }
        }
        return res;
    }
    public static void m1(List<T1> data) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
        for(T1 o : data) {
            if(o.getF1() < 3) {
                System.out.printf("m1: %d %s\n", o.get("f1"), o.get("f2"));
            }
        }
    }
    public static void m2(List<T1> data) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
        for(T1 o : data) {
            if(o.getF1() >= 3) {
                System.out.printf("m2: %d %s\n", o.get("f1"), o.get("f2"));
            }
        }
    }
    public static void main(String[] args) throws SQLException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
        List<T1> data = loadData();
        m1(data);
        m2(data);
    }
}
