title: In Java, is it better to avoid explicitly using database column names, data types etc?
body: <p>Is it considered better practice to avoid explicitly using (hardcoding) database column headers, data types etc. when fetching results from a database table? What I'm getting at is - is it better to pretend like I know absolutely nothing about the way the database is structured and let the program work that out (thus making it longer and more complicated), or is it perfectly fine to just use it directly in the code (or possibly list most information as constants at the top of the class)?</p>
  
  <p>For example, there are these two ways of retrieving data from a table.</p>
  
  <p>The simple way being:</p>
  
  <pre><code>ArrayList&lt;String&gt; userNames = new ArrayList&lt;&gt;();
  ArrayList&lt;Blob&gt; profilePictures = new ArrayList&lt;&gt;();
  
  // this method presumes that there are columns named user_name and 
  // profile_picture and that they hold string and blob values
  // respectively
  
  while(resultSet.next()) {
      userNames.add(resultSet.getString("user_name"));
      profilePictures.add(resultSet.getBlob("profile_picture"));
  }
  </code></pre>
  
  <p>... and the other way being:</p>
  
  <pre><code>ResultSetMetaData meta = resultSet.getMetaData();
  
  ArrayList&lt;String&gt; columnHeaders = new ArrayList&lt;&gt;();
  
  // doesn't know what the column headers are
  for (int i = 1; i &lt;= meta.getColumnCount(); i++) {
      columnHeaders.add(meta.getColumnName(i));
  }
  
  ArrayList&lt;ArrayList&lt;Object&gt;&gt; data = new ArrayList&lt;&gt;();
  
  for(int i=0; i&lt;columnHeaders.size(); i++) {
      data.add(new ArrayList&lt;Object&gt;());
  }
  
  for(int i=0; resultSet.next(); i++) {
      for (String header : columnHeaders) {
          // no presuming what the data type will be
          int type = meta.getColumnType(i);
          if(type == Types.VARCHAR) {
               data.get(i).add(resultSet.getString(header));
           } else if(type == Types.BLOB) {
              data.get(i).add(resultSet.getBlob(header));
           }
      }
  }
  </code></pre>
  
  <p>The first way is short and sweet, but the other way feels...smarter(?).</p>
  
  <p>Is there justifiable reason for doing things the hard way, or am I just overcomplicating things?</p>