2

I'm very new to Spring Boot (or even to Spring). I want to create a Singleton class that controls my jdbc database. After (the successfull tests) I tried to separate my main class and this DBHandler class, I get a NullPointerException when I try to call a method on my database object in my DBHandler class.
Maybe I need some annotation on my DBHandler class? Is this the proper way to create a database handler class in Spring Boot?

Main class:

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

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String args[]) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... strings) throws Exception {
        DBHandler db = DBHandler.getDBHandler();
        db.deleteTable();
        db.createTable();
    }
}

JDBC Handler Singleton class that I want to fill with functions to interact with my database that I can call from other classes.

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

public class DBHandler {

    @Autowired
    JdbcTemplate database;

    private static DBHandler instance;

    private DBHandler() {
    }

    public static DBHandler getDBHandler() {
        if (instance == null) {
            synchronized (DBHandler.class) {
                if (instance == null) {
                    instance = new DBHandler();
                }
            }
        }
        return instance;
    }

    public void createTable() {
        database.execute("CREATE TABLE users(id SERIAL, accountName VARCHAR(255), validated VARCHAR(255), validationCode VARCHAR(255), updateDate DATETIME)");
    }

    public void deleteTable() {
        database.execute("DROP TABLE IF EXISTS users");
    }
}

Everything works fine when I have my Autowired database and the functions in the main (Application) class.

2
  • 1
    Did you check if DBHandler is coming as null , resulting null pointer exception? Ideally you would not want to declare static stuff in DBHandler, instead you can have your database methods and access them in main class by Autowiring DBHandler. Commented Feb 19, 2019 at 0:54
  • DBHandler was getting instancialized, tested it with a print out function. Your solution is what I was looking for as Deadpool demonstrated it below, thank you! Commented Feb 19, 2019 at 11:19

1 Answer 1

2

By default spring creates singleton bean so leave that to spring Application context, Annotate DBHandler with @Component

DBHandler

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

@Component
public class DBHandler {

@Autowired
JdbcTemplate database;

public void createTable() {
    database.execute("CREATE TABLE users(id SERIAL, accountName VARCHAR(255), validated VARCHAR(255), validationCode VARCHAR(255), updateDate DATETIME)");
}

public void deleteTable() {
    database.execute("DROP TABLE IF EXISTS users");
    }
}

Application

@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private DBHandler dBHandler;

public static void main(String args[]) {
    SpringApplication.run(Application.class, args);
}

@Override
public void run(String... strings) throws Exception {
    db.deleteTable();
    db.createTable();
    }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, this is exactly what I was looking for! Everything works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.