title: Spring NamedParameterJDBCTemplate reuse of Prepared Statements
body: <p>I am using the Spring NamedParameterJdbcTemplate to fetch some values from a table. For some reason, the query runs very slow in my Java app as opposed to running the same query on SQL Management Studio. I also noticed in the profiler, the prepared statements don't get reused. If I run the same query in my JAVA app multiple times, I see different prepared statements being executed. So, not sure why the statements are not getting reused.
  Is the performance slow because I am using a IN clause in my query?</p>
  
  <p>Here is my sample java code</p>
  
  <pre><code>StringBuilder vQuery = new StringBuilder();
  vQuery.append(" SELECT SUM(Qty) FROM vDemand");
  vQuery.append(" WHERE ProductID = :ProductID");
  vQuery.append(" AND [Date] &gt;= :StartDate AND [Date] &lt;= :EndDate");
  vQuery.append(" AND CustomerID IN ( :CustomerID )");
  
  MapSqlParameterSource vNamedParameters = new MapSqlParameterSource();
  vNamedParameters.addValue("ProductID", aProductID);
  vNamedParameters.addValue("CustomerID", aCustomerlIDs);
  vNamedParameters.addValue("StartDate", aDate, Types.TIMESTAMP);
  vNamedParameters.addValue("EndDate", aDate, Types.TIMESTAMP);
  
  int vTotalQuantity = this.getNamedParameterJdbcTemplate().queryForInt(vQuery.toString(), vNamedParameters);
  return vTotalQuantity;
  </code></pre>