-1

Unable to connect Postgres database to Tomcat 10.0.27 server Here is DBConnection code:

public class DBConnection {

    public static Connection getConnectionToDatabase() {
        Connection connection = null;

        try {
            System.out.println("MySQL JDBC Driver Registered!");

            connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/hplus", "postgres", "");
        }

        catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();

        }

        if (connection != null) {
            System.out.println("Connection made to DB!");
        }
        return connection;
    }

}

Here is the code, where i try to get the connection:

public class ApplicationDao {

    public List<Product> searchProducts(String searchString) {
        Product product = null;
        List<Product> products = new ArrayList<>();
        try{
            Connection connection = DBConnection.getConnectionToDatabase();

            String sql = "select * from products where product_name like '%"+searchString+"%'";

            Statement statement = connection.createStatement();

            ResultSet set = statement.executeQuery(sql);

            while(set.next()){
                product= new Product();
                product.setProductId(set.getInt("product_id"));
                product.setProductImgPath(set.getString("image_path"));
                product.setProductName(set.getString("product_name"));
                products.add(product);

            }

        }
        catch(SQLException exception){
            exception.printStackTrace();
        }
        return products;
    }

}

Here is what i get:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message Cannot invoke "java.sql.Connection.createStatement()" because "connection" is null

I tried to debug the code but got no response. I looked on different forms what the problem is, but I did not find anything

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
wxist
  • 131
  • 5
  • Have you "Checked the output console" as your own error message suggests? – dinhokz Feb 08 '23 at 16:29
  • 1
    You print "MySQL JDBC Driver Registered!" - but you are connecting to postgres. Are you sure you got the correct JDBC Driver? Whats the error message? And you should close your connection somewhere. – TomStroemer Feb 08 '23 at 16:30
  • 1
    @dinhokz No suitable driver found for jdbc:postgresql://localhost:5432/hplus – wxist Feb 08 '23 at 16:30
  • The error tells you that `connection` is `null`. You need to debug your code to find out why. – Code-Apprentice Feb 08 '23 at 16:30
  • @TomStroemer Dont care about text since i'm learning servlets and jsp now and trying to open up project on my laptop but using postgres database – wxist Feb 08 '23 at 16:32
  • 3
    Add the postgres jdbc driver as dependency (maven, gradle, classpath, whatever you are using ...). – TomStroemer Feb 08 '23 at 16:32
  • @Code-Apprentice No suitable driver found for jdbc:postgresql://localhost:5432/hplus – wxist Feb 08 '23 at 16:32
  • So you need to install the correct driver. – Code-Apprentice Feb 08 '23 at 16:32
  • @TomStroemer Done, i added as maven dependency, but nothing changed... – wxist Feb 08 '23 at 16:33
  • "System.out.println("Connection made to DB!");" Did this print out to console when you ran the project ? – Dennis LLopis Feb 08 '23 at 16:36
  • @DennisLLopis No – wxist Feb 08 '23 at 16:37
  • what about either try/catch printouts ? – Dennis LLopis Feb 08 '23 at 16:38
  • @DennisLLopis Here is the text from logs: No suitable driver found for jdbc:postgresql://localhost:5432/hplus – wxist Feb 08 '23 at 16:43
  • @DennisLLopis i tried to make a directory /WEB-INF/lib and pass posgres.jar there, but it didnt help, also tried to add maven dependency and tried to upload postgres.jar file in tomcat/lib directory, but all of this does not help – wxist Feb 08 '23 at 16:45
  • Honestly your code is working fine for me, I installed postgres and added the postgresjdbc driver to build path in my project (which I believe is your issue). I created a table which the same definitions(products) and successfully ran the build. Which driver did you download? – Dennis LLopis Feb 08 '23 at 17:35
  • @DennisLLopis Could you show what you have done in the answer? I downloaded postgresql-42.2.5 – wxist Feb 08 '23 at 17:47
  • You shouldn't catch exceptions and then continue on as if nothing bad happened. You need to take corrective action, wrap the exception in another exception, or let the exception bubble up to a place where you can take appropriate action. – Mark Rotteveel Feb 09 '23 at 10:29

1 Answers1

0

The problem was in incorrect driver. Everything work out after adding this lines of code in DBConnection class:

        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
// ...
wxist
  • 131
  • 5