title: How to use Spring's JdbcTemplate to connect to a simple MySql database?
body: <p>I am trying to use Spring's JdbcTemplate Class to connect to a simple MySql database based on <a href="http://spring.io/guides/gs/relational-data-access/" rel="noreferrer">this</a> tutorial. In fact, I used their project setup:</p>
  
  <p>pom.xml </p>
  
  <pre><code>    &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;test&lt;/groupId&gt;
      &lt;artifactId&gt;jdbc-test&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.2.2.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&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;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;version&gt;1.14.8&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>The Lombok dependency is for getters and setters.</p>
  
  <p>Then there is the Application class:</p>
  
  <pre><code>package test;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.jdbc.core.JdbcTemplate;
  
  @SpringBootApplication
  public class Application implements CommandLineRunner {
      public static void main(String args[]) {
          SpringApplication.run(Application.class, args);
      }
  
      @Autowired
      JdbcTemplate jdbcTemplate;
  
      @Override
      public void run(String... arg0) throws Exception {
          System.out.println("Creating tables");
          jdbcTemplate.execute("drop table customers if exists");
          jdbcTemplate.execute("create table customers(" +
                  "id serial, first_name varchar(255), last_name varchar(255))");
      }
  }
  </code></pre>
  
  <p>And finally a Customer pojo:</p>
  
  <pre><code>package test;
  
  import lombok.AllArgsConstructor;
  import lombok.Data;
  
  @Data
  @AllArgsConstructor
  public class Customer {
      private long id;
      private String firstName, lastName;
  }
  </code></pre>
  
  <p>For datasource configuration I have an application.properties file in my resources folder:</p>
  
  <pre><code>spring.datasource.url=jdbc:mysql://localhost:3306/test
  spring.datasource.username=test
  spring.datasource.password=test
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  
  spring.jpa.database = MYSQL
  
  spring.jpa.show-sql = true
  
  spring.jpa.hibernate.ddl-auto = update
  spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
  </code></pre>
  
  <p>The database is up and running and still the autowirig dosen't seem to work.
  Here is the exception I get when I try to run it:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.
  BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate test.Application.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
  {@org.springframework.beans.factory.annotation.Autowired(required=true)}
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
          at test.Application.main(Application.java:12)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
          at java.lang.Thread.run(Unknown Source)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate test.Application.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
  No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire
  candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
          ... 21 common frames omitted
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which
   qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
          ... 23 common frames omitted
  </code></pre>
  
  <p>What I find curious is the fact that if I replace the mysql-connector-java dependeny with a com.h2database, it just works (as in the spring example).</p>
  
  <p>Do I need to configure the mysql datasource in a dirfferent way?
  Any suggestions?</p>