// 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.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ToCloseOrNotToclose {
    // Approach 1:
    // * close everything as soon as possible in good flow
    // * close in catch block if necessary to handle bad flows
    public static void approach1(String cond) {
        Connection con = null;
        PreparedStatement upd1 = null;
        PreparedStatement upd2a = null;
        PreparedStatement upd2b = null;
        try {
             con = DriverManager.getConnection("db_connect_string", "db_userid", "db_password");
             upd1 = con.prepareStatement("initial query");
             upd1.executeQuery();
             upd1.close();
             if (cond.equals("one")) {
                 upd2a = con.prepareStatement("query something");
                 upd2a.executeQuery();
                 upd2a.close();
             } else {
                 upd2b = con.prepareStatement("query something else");
                 upd2b.executeQuery();
                 upd2b.close();
             }
             con.close();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                if(upd1 != null && !upd1.isClosed()) upd1.close();
                if(upd2a != null && !upd2a.isClosed()) upd2a.close();
                if(upd2b != null && !upd2b.isClosed()) upd2b.close();
                if(con != null && !con.isClosed()) con.close();
            } catch (SQLException e2) {
                e2.printStackTrace();
                // consider what to do here
            }
            // consider what to do here
        }
    }
    // Approach 2:
    // * close in finally block for both good and bad flows
    public static void approach2(String cond) {
        Connection con = null;
        PreparedStatement upd1 = null;
        PreparedStatement upd2a = null;
        PreparedStatement upd2b = null;
        try {
             con = DriverManager.getConnection("db_connect_string", "db_userid", "db_password");
             upd1 = con.prepareStatement("initial query");
             upd1.executeQuery();
             if (cond.equals("one")) {
                 upd2a = con.prepareStatement("query something");
                 upd2a.executeQuery();
             } else {
                 upd2b = con.prepareStatement("query something else");
                 upd2b.executeQuery();
             }
             con.close();
        } catch (SQLException e) {
            e.printStackTrace();
            // consider what to do here
        } finally {
            try {
                if(upd1 != null) upd1.close();
                if(upd2a != null) upd2a.close();
                if(upd2b != null) upd2b.close();
                if(con != null) con.close();
            } catch (SQLException e2) {
                e2.printStackTrace();
                // consider what to do here
            }
        }
    }
    // Approach 3:
    // * use Java 7+ try with resource
    public static void approach3(String cond) {
        try (Connection con = DriverManager.getConnection("db_connect_string", "db_userid", "db_password")) {
             try(PreparedStatement upd1 = con.prepareStatement("initial query")) {
                 upd1.executeQuery();
             }
             if (cond.equals("one")) {
                 try(PreparedStatement upd2a = con.prepareStatement("query something")) {
                     upd2a.executeQuery();
                 }
             } else {
                 try(PreparedStatement upd2b = con.prepareStatement("query something else")) {
                     upd2b.executeQuery();
                 }
             }
        } catch (SQLException e) {
            e.printStackTrace();
            // consider what to do here
        }
    }
}
