0

So I'm developing a database project in which I created my database, filled it with my tables, and am now starting to program a Java application to demonstrate it.

Now comes the problem: I don't stop getting the "java.lang.ClassNotFoundException: org.postgresql.Driver" gigantic error prompt or the "No suitable driver found for jdbc:postgresql://localhost:5432/project" prompt.

//File: Model.java
import java.sql.*;

class Model {
    private final String url = "jdbc:postgresql://localhost:5432/Trabalho";
    private final String usr = "postgres";
    private final String pwd = "K1dN@m3dF1ng3r";

    public Model() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch(ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    protected Connection connect() {
        Connection con = null;

        try {
            con = DriverManager.getConnection(url,usr,pwd);
            System.out.println("Connected to the PostgreSQL server successfully!");
        } catch(SQLException ex) {
            System.out.println(ex.getMessage());
        }

        return con;
    }
}
//File: Control.java
class Control extends Model {
    public Control() {
        super.connect();
    }

    public static void main(String tx[]) {
        new Control();
    }
}

I have tried downloading the driver, searching for my lib folder, altering my classpath environment variable, I've checked PGAdmin, java, javac and the driver's versions, but nothing seems to work.

Also, most of the Qs/As here (all that I've found) seem to focus on Java IDEs that I don't use. I'm using VS Code, with its 0.25.11 Extension Pack for Java.

ChaimCad
  • 1
  • 1
  • [Class.forName(JDBC_DRIVER) no longer needed?](https://stackoverflow.com/questions/28220724/class-fornamejdbc-driver-no-longer-needed) – Abra Jun 28 '23 at 05:12
  • 1
    I'm guessing that the JDBC driver JAR file is not on the CLASSPATH (or module path). What is the name of the JDBC driver JAR file? What Java version are you using? – Abra Jun 28 '23 at 05:16
  • @Abra Although it is not required, it still works (in fact, automatic driver-loading is just a fancy way of getting the Java ServiceLoader to call `Class.forName` for you), and it is a good way to diagnose the problem, aas in this case the throwing of `ClassNotFoundException` makes clear the driver is not on the classpath. – Mark Rotteveel Jun 28 '23 at 16:09
  • How are you **running** your application? – Mark Rotteveel Jun 28 '23 at 16:10
  • @MarkRotteveel It saves you from making a mistake when writing the name of the class to load. That's probably why the functionality was added. Maybe the OP put the JAR file in the correct folder but made a mistake with the name of the class to load. – Abra Jun 28 '23 at 18:24

1 Answers1

0

The connection driver class is missing which you should import into classpath. You can download jar file of that class with all dependencies in a below link

https://jar-download.com/artifacts/org.postgresql/postgresql

Import that jar file into you project. If you do not know how to import then look out the thread.

Visual Studio Code, Java Extension, how to add a JAR to classpath?

Alpha
  • 167
  • 1
  • 13