title: How exactly does queryForMap() and queryForList() methods provided by the Spring JdbcTemplate work internally?
body: <p>I am studying for the Spring Core certification and I have some doubt related to the use of the <strong>JdbcTemplate</strong> use.</p>
  
  <p>I know that JdbcTemplate gives me different methods to perform queries (that are written in SQL).</p>
  
  <p>So I have the following methods:</p>
  
  <p>1) <strong>queryForObject()</strong>:</p>
  
  <pre><code>public long getPersonCount() {
      String sql = “select count(*) from PERSON”;
      return jdbcTemplate.queryForObject(sql, Long.class);
  }
  </code></pre>
  
  <p>So in this method I specify 2 parameters that represent respectively the SQL statement and the <strong>class</strong> type of the returned object. In the previous case it is specified that the result of the query is returned into a <strong>Long</strong> object.</p>
  
  <p>What happens if the result returned from the query doesn't match with the specified type? (for example if the query returns a String and I have specified a Long as parameter of the queryForObject() method? How are situations like this handled?</p>
  
  <p>2) <strong>queryForMap()</strong>:</p>
  
  <pre><code>public Map getPersonInfo(int id) {
      String sql = “select * from PERSON where id=?”;
      return jdbcTemplate.queryForMap(sql, id);
  }
  </code></pre>
  
  <p>Reading the documentation it seems to me that this is used only when it is known that my query returns a <strong>single row</strong>. Is it correct?</p>
  
  <p>So now I have a doubt related the use of the <strong>queryForMap()</strong> method.</p>
  
  <p>I know that the <strong>Map</strong> interface stores objects using <strong></strong> system.</p>
  
  <p>So a <strong>Map</strong> is thought to store multiple <strong></strong> couples. So I think that this could store multiple rows: the key of the Map is the key of a row of my table and the value contains the values of the other columns (into an object).</p>
  
  <p>But it seems that the logic of the <strong>queryForMap()</strong> method is pretty different. How does it work exactly?</p>
  
  <p>Maybe it returns a Map that works in this way:</p>
  
  <p><strong>key</strong>: contains the key of the primary key of the table and with this key are associated multiple values related to the content of the other fields of my table. Or what?</p>
  
  <p>3) <strong>queryForList()</strong>:</p>
  
  <pre><code>public List getAllPersonInfo() {
      String sql = “select * from PERSON”;
      return jdbcTemplate.queryForList(sql);
  }
  </code></pre>
  
  <p>Reading the documentation it seems to me that it is used when I expect that my query returns <strong>multiple rows</strong>. And it seems to me that the method output is a <strong>List of Map</strong> (so according to the previous method each Map represents a single returned row** (but the previous doubt about how a row is stored into a Map persists).</p>
  
  <p>Can you help me to understand deeply how these methods exactly work?</p>
  
  <p>Tnx</p>