title: Connection to database with JdbcTemplate
body: <p>I'm trying to create table (h2) with jdbcTemplate. Everything is ok when I execute different queries in UsersDAOImpl class, but when I try to create table first in Application class, the JdbcTemplate can't connect to the database. I read that I need to add dependency group spring-boot-starter-jdbc that will automatically generate dataSource to witch my JdbcTemplate should connect, but it seems that doesn't work. I think that I missed something but can't find what.</p>
  
  <p>Application class:</p>
  
  <pre><code>@SpringBootApplication
  public class Application implements CommandLineRunner  {
  
  public static void main(String[] args) throws Exception{
      SpringApplication.run(Application.class, args);
  }
   //doesn't create db alone;
  @Autowired
  JdbcTemplate jdbcTemplate;
  
  
  @Override
  public void run(String... arg0) throws Exception {
      jdbcTemplate.execute("DROP TABLE test IF EXISTS");
      jdbcTemplate.execute("CREATE TABLE test( id int(11), name VARCHAR(255),      role VARCHAR(255))");
  }
  }
  </code></pre>
  
  <p>UsersDAOImpl class:</p>
  
  <pre><code>public class UsersDAOImpl implements UsersDAO {
  
  private DataSource dataSource;
  
  public void setDataSource(DataSource dataSource) {
      this.dataSource = dataSource;
  }
  //and some additional methods to work with the database
  }
  </code></pre>
  
  <p>Controller class:</p>
  
  <pre><code>@RestController
  class Controller {  
  
      ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
      UsersDAO userDAO = ctx.getBean("userDAO", UsersDAO.class);
   //making the mapping
  }
  </code></pre>
  
  <p>spring.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
  &lt;bean id="userDAO" class="hello.UsersDAOImpl"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
      &lt;property name="url" value="jdbc:h2:~/test" /&gt;
      &lt;property name="username" value="sa" /&gt;
      &lt;property name="password" value="" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.h2database&lt;/groupId&gt;
          &lt;artifactId&gt;h2&lt;/artifactId&gt;
      &lt;/dependency&gt;   
  &lt;/dependencies&gt;
  </code></pre>