// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.math.BigDecimal;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class ListJDBC {
    private static void perform(String clznam, String conurl, String usr, String pwd) throws Exception {
        System.out.println(conurl);
        Class.forName(clznam);
        Connection con = DriverManager.getConnection(conurl, usr, pwd);
        Statement stmt = con.createStatement();
        ResultSet rspot = stmt.executeQuery("SELECT customer.id AS cid, name, phone, source " +
                                            "FROM customer JOIN customer_potential ON customer.id = customer_potential.id " +
                                            "ORDER BY cid");
        while(rspot.next()) {
            String customer_name = rspot.getString(2);
            String customer_phone = rspot.getString(3);
            String customer_potential_source = rspot.getString(4);
            System.out.printf("**future customer** %s, %s, %s\n", customer_name, customer_phone, customer_potential_source);
        }
        rspot.close();
        ResultSet rsact = stmt.executeQuery("SELECT customer.id AS cid,name,address1,address2,phone,order_main.id AS oid,status,SUM(quantity*price) AS sumval " +
                                            "FROM customer JOIN customer_actual ON customer.id = customer_actual.id " +
                                            "              JOIN order_main ON customer.id = order_main.customer " +
                                            "              JOIN order_line ON order_main.id = order_line.order_main " +
                                            "GROUP BY customer.id,name,address1,address2,phone,order_main.id,status " +
                                            "ORDER BY cid, oid");
        int last_customer_id = 0;
        while(rsact.next()) {
            int customer_id = rsact.getInt(1);
            String customer_name = rsact.getString(2);
            String customer_actual_address1 = rsact.getString(3);
            String customer_actual_address2 = rsact.getString(4);
            String customer_phone = rsact.getString(5);
            int order_id = rsact.getInt(6);
            String order_status = rsact.getString(7);
            BigDecimal sumval = rsact.getBigDecimal(8);
            if(customer_id != last_customer_id) {
                System.out.printf("%s, %s, %s, %s\n", customer_name.trim(), customer_actual_address1.trim(), customer_actual_address2.trim(), customer_phone.trim());
                last_customer_id = customer_id;
            }
            System.out.printf("  %d, %s, %.2f\n", order_id, order_status.trim(), sumval);
        }
        rsact.close();
        stmt.close();
        con.close();
    }
    public static void main(String[] args) throws Exception {
        if(args.length == 4) {
            perform(args[0], args[1], args[2], args[3]);
        } else if(args.length == 2) {
            perform(args[0], args[1], null, null);
        }
    }
}
