StatementRental.java

package edu.hawaii.ics.yucheng;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

interface StatementRunner {
    void run(Statement statement) throws ProgramException, SQLException;
}

final class StatementFactory {
    public static void newStatement(ConfigurationNode node, StatementRunner runner) throws ProgramException {
        try {
            final Connection connection = node.getConnection();

            try {
                final Statement statement = connection.createStatement();
                try {
                    runner.run(statement);
                } finally {
                    statement.close();
                }
            } finally {
                connection.close();
            }
        } catch (final SQLException e) {
            throw new ProgramException(e);
        }
    }
}
Valid HTML 4.01 Valid CSS