title: Is there a cleaner way to use try-with-resource and PreparedStatement?
body: <p>Here is the <code>Main.java</code>:</p>
  
  <pre><code>package foo.sandbox.db;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  public class Main {
      public static void main(String[] args) {
          final String SQL = "select * from NVPAIR where name=?";
          try (
                  Connection connection = DatabaseManager.getConnection();
                  PreparedStatement stmt = connection.prepareStatement(SQL);
                  DatabaseManager.PreparedStatementSetter&lt;PreparedStatement&gt; ignored = new DatabaseManager.PreparedStatementSetter&lt;PreparedStatement&gt;(stmt) {
                      @Override
                      public void init(PreparedStatement ps) throws SQLException {
                          ps.setString(1, "foo");
                      }
                  };
                  ResultSet rs = stmt.executeQuery()
          ) {
              while (rs.next()) {
                  System.out.println(rs.getString("name") + "=" + rs.getString("value"));
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>And here is <code>DatabaseManager.java</code></p>
  
  <pre><code>package foo.sandbox.db;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  /**
   * Initialize script
   * -----
   * CREATE TABLE NVPAIR;
   * ALTER TABLE PUBLIC.NVPAIR ADD value VARCHAR2 NULL;
   * ALTER TABLE PUBLIC.NVPAIR ADD id int NOT NULL AUTO_INCREMENT;
   * CREATE UNIQUE INDEX NVPAIR_id_uindex ON PUBLIC.NVPAIR (id);
   * ALTER TABLE PUBLIC.NVPAIR ADD name VARCHAR2 NOT NULL;
   * ALTER TABLE PUBLIC.NVPAIR ADD CONSTRAINT NVPAIR_name_pk PRIMARY KEY (name);
   *
   * INSERT INTO NVPAIR(name, value) VALUES('foo', 'foo-value');
   * INSERT INTO NVPAIR(name, value) VALUES('bar', 'bar-value');
   */
  public class DatabaseManager {
      /**
       * Class to allow PreparedStatement to initialize parmaters inside try-with-resource
       * @param &lt;T&gt; extends Statement
       */
      public static abstract class PreparedStatementSetter&lt;T extends Statement&gt; implements AutoCloseable {
          public PreparedStatementSetter(PreparedStatement pstmt) throws SQLException {
              init(pstmt);
          }
  
          @Override
          public void close() throws Exception {
          }
  
          public abstract void init(PreparedStatement pstmt) throws SQLException;
      }
  
      /* Use local file for database */
      private static final String JDBC_CONNECTION = "jdbc:h2:file:./db/sandbox_h2.db;MODE=PostgreSQL";
  
      static {
          try {
              Class.forName("org.h2.Driver");  // Init H2 DB driver
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      /**
       * @return Database connection
       * @throws SQLException
       */
      public static Connection getConnection() throws SQLException {
          return DriverManager.getConnection(JDBC_CONNECTION, "su", "");
      }
  }
  </code></pre>
  
  <p><em>I am using H2 database for simplicity since it's a file based one that is easy to create and test on.</em></p>
  
  <p>So everything works and resources get cleaned up as expected, however I just feel there may be a cleaner way to set the <code>PreparedStatement</code> parameters from inside the try-with-resources block (and I don't want to use nested try/catch blocks as those look 'awkward').  Maybe there already exists a helper class in JDBC that does just this, but I have not been able to find one.</p>
  
  <p>Preferably with a lambda function to initialize the <code>PreparedStatement</code> but it would still require allocating an <code>AutoCloseable</code> object so it can be inside the try-with-resources.</p>