How to run methods inside a standard try catch
<p>It's been a while since I have done any Java programming. And I find my self a bit stuck. </p>

<p>My problem is that I have a pooled db connection in tomcat. That is working nicely. But there is a lot of boiler plate required. </p>

<pre><code>    public void init() {
    Connection conn = null;
    ResultSet rst = null;
    Statement stmt = null;
    try {

        //SETUP 
        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env/jdbc");
        OracleDataSource ds = (OracleDataSource) envContext.lookup("tclsms");

        if (envContext == null) throw new Exception("Error: No Context");
        if (ds == null) throw new Exception("Error: No DataSource");
        if (ds != null) conn = ds.getConnection();
        if (conn == null) throw new Exception("Error: No Connection")

        message = "Got Connection " + conn.toString() + ", ";

        //BODY 
        stmt = conn.createStatement();
        rst = stmt.executeQuery("SELECT 'Success obtaining connection' FROM DUAL");

        if (rst.next()) message = rst.getString(1);



        //TEAR DOWN 
        rst.close();
        rst = null;
        stmt.close();
        stmt = null;
        conn.close(); // Return to connection pool
        conn = null; // Make sure we don't close it twice
    } catch (Exception e) {
        e.printStackTrace();
        //TODO proper error handling 
    } finally {
        // Always make sure result sets and statements are closed,
        // and the connection is returned to the pool
        if (rst != null) {
            try {
                rst.close();
            } catch (SQLException e) {;}
            rst = null;
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {;}
            stmt = null;
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {;}
            conn = null;
        }
    } //END FINALLY
} //END INIT
</code></pre>

<p>So I want to do the equivalent of passing a method into init that will run in the body of the function. I know I can't do this in Java. But I'm sure there must be a nice way to do this. Or at least a best practice for this sort of thing.</p>

<p>Any help much appreciated.</p>
