-1

I'm trying to implement a singleton DB connection in Java. I think I have it mostly correct, but when I try to use it in other classes, I keep getting java.lang.NullPointerExceptions.

The error I'm getting is in my ProduitDaoImpl class, on the PreparedStatement line where I do connection.prepareStatement(query); I think connection is being passed as null, but I don't know why or how to fix it.

here is my ProduitDaoImpl

public class ProduitDaoImpl implements IProduitDao {



    public List<Produit> produitsParMC(String mc) {
        List<Produit> produits=new ArrayList<Produit>();
        Connection connection=SingletonConnection.getConnection();
        try {




            PreparedStatement ps=connection.prepareStatement("SELECT * FROM PRODUITS WHERE DESIGNATION LIKE ?");
            ps.setString(1, mc);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){

                Produit p=new Produit();
                p.setId(rs.getLong("ID"));
                p.setDesignation(rs.getString("DESIGNATION"));
                p.setPrix(rs.getDouble("Prix"));
                p.setQuantite(rs.getInt("QUANTITE"));
                produits.add(p);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return produits;
    }







}

and here is my singleton

public class SingletonConnection {
    private static Connection connection;
    //le block static charge la classe en mémoire lors de son appel
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            try {
                connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db-natal","root","");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public static Connection getConnection() {
        return connection;
    }

}
marouu
  • 17
  • 5

1 Answers1

0

Your Singleton implementation is incorrect. (If you are new to this, which I assume you are, then please refer to https://medium.com/@kevalpatel2106/how-to-make-the-perfect-singleton-de6b951dfdb0 or any other guide online available to do so.)

Zlxo, creating a singleton connection object to be shared across the application by all users is not a good approach for various factors.

Regardless, please check below for a possible things you can check in your code;

Is connection object really being created correctly by the singleton class?
Are the credentials in the SingletonConnection class correct?

Add System.out.println(connection); before PreparedStatement ps=connection.prepareStatement(<your query>); in the ProduitDaoImpl class.

Does it print null or some string?

If it is null, then perhaps recheck the connect string you are using to get the connection object in the singleton class.

Do vote up this answer if you found it helpful! :)

Shivam Puri
  • 1,578
  • 12
  • 25