title: Converting JDBC ResultSet to generic type?
body: <p>I am trying to build a simple, lightweight "DAO layer" for my app that just uses pure JDBC (nothing fancy like Hibernate, TopLink, MyBatis, etc.).</p>
  
  <pre><code>public class AnimalDAO extends BaseDAO&lt;Animal&gt; {
      private AnimalType type;
      private String name;
  
      public void insertAnimal(Animal animal) {
          StingBuilder sql = new StringBuilder();
          sql.append("INSERT INTO animals (");
          sql.append("animal_type_id, animal_name ) VALUES (");
          sql.append(animal.getType().getId());
          sql.append(animal.getName());
          sql.append(" )");
  
          doSQL(sql.toString());
      }
  }
  
  public class BaseDAO&lt;T&gt; {
      private Long id;
  
      protected T doSQL(String sql) {
          // Typical JDBC code to hit a database with the "sql" query and return a ResultSet.
          ResultSet result = queryDatabase(sql);
  
          // Now, how do I turn "result" into type T such that, in the case of 
          // AnimalDAO#insertAnimal(Animal), result gets converted into an Animal?
          return ???;
      }
  }
  </code></pre>
  
  <p>So the idea is to have a <code>doSQL(String)</code> method in the <code>BaseDAO</code> that will take care of all the boilerplate JDBC stuff (making prepared statements, connection pooling, try-catch blocks for transactional commits, etc.). Then, subclasses like <code>Animal</code> just need to tell <code>doSQL</code> which query to execute.</p>
  
  <p>The problem is, I'm not sure how to convert the JDBC <code>ResultSet</code> into the appropriate <code>T</code> generic type, which in the case of <code>AnimalDAO</code> is <code>Animal</code>. Any ideas? Thanks in advance!</p>