0

First off: I have searched about every article and every question I could find for 3 hours straight now. I just can't figure out how to get the JDBC connection running using Spring.

I am using Gradle so without further ado here is my build.gradle

plugins {
  id 'org.springframework.boot' version '2.3.4.RELEASE'
  id 'io.spring.dependency-management' version '1.0.10.RELEASE'
  id 'java'
}

group = 'my.self'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
  mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'

    runtimeOnly 'mysql:mysql-connector-java'

    testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
  useJUnitPlatform()
}

The Class containing my main method:

package my.self.SpringDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class, args);
        Tester a = new Tester();
    }
}

And my tester class were I Autowired the JdbcTemplate Class

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class Tester {

    @Autowired
    JdbcTemplate jdbc;

    public Tester(){
        int result = jdbc.queryForObject("SELECT COUNT(*) FROM Customer", Integer.class);
        System.out.println(result);
    }
}

The application.config file

spring.datasource.url=jdbc:mysql://localhost:3306/sks?serverTimezone=UTC
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

However i always get a NullPointerException in this line:

int result = jdbc.queryForObject("SELECT COUNT(*) FROM Customer", Integer.class);

I cant figure it out and none of the articles etc, have helped me so far. They dont really explain anything besides the things i have already done. Autowiring the JdbcTemplate etc...

The statement is valid too since i get the result of 1 when i manually execute this query on the MySql db. ( only 1 entry )

Its pretty much a big question mark after all those hours still.

I am desperate for help... regard ^^

Andrzej Sydor
  • 1,373
  • 4
  • 13
  • 28
MajesticOl
  • 311
  • 1
  • 20
  • 3
    First problem: class Tester does not have `@Component` or `@Service` annotation and therefore `@Autowired` will not work. – Alexandra Dudkina Oct 08 '20 at 18:00
  • 2
    Second problem: `@Autowired` fields are injected after invoking constructor. Even if class Tester were managed by Spring, variable `jdbc` would be null. Call `jdbc.queryForObject()` should be done not in constructor, but in a method. – Alexandra Dudkina Oct 08 '20 at 18:02
  • First off thanks for the help . So i have added the @Component annotation to my Tester class and creadted a simple tester method with the query call. I still get the same exception though `import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; @Component public class Tester { @Autowired JdbcTemplate jdbc; public void tester(){ int result = jdbc.queryForObject("SELECT COUNT(*) FROM Customer", Integer.class); System.out.println(result); } }` – MajesticOl Oct 08 '20 at 18:11
  • @AlexandraDudkina It's not _required_ to have an annotation; I normally don't these days, preferring `@Import` and `@Bean` instead. – chrylis -cautiouslyoptimistic- Oct 08 '20 at 18:15
  • OP, you're still calling `new`. Don't do that; if you want to use the `Tester` to do something, create a `CommandLineRunner` and inject the `Tester` there. (Note also that converting to constructor injection will make your life much better.) – chrylis -cautiouslyoptimistic- Oct 08 '20 at 18:16
  • Thank you very much ! I think i understand Spring better now ! – MajesticOl Oct 08 '20 at 18:25

0 Answers0