java.lang.Runtimeexception: java.lang.Classnotfoundexception: org.postgresql.Driver

Asked

Viewed 1,682 times

1

I created the DAO, MODEL, CONTROLLER packages and a test It runs normal with the server, persists in the normal bank but through the viewing layer. I wanted to test in main running with java application and set the values of the car before building the rest as jsp pages to see if there are any errors.

In the test class of the TEST package is like this:

import model.Carro;
import dao.CarroDAO;

public class Teste {

    public static void main (String[] args){

    Carro carro = new Carro();

    carro.setMarca("Chevrolet");
    carro.setModelo("Cruze");
    carro.setChassi(2.4);
    carro.setRenavam(2.5);
    carro.setMotor(1.6F);

    new CarroDAO().save(carro); 
 }  
}

Only when I’m making these exceptions:

Exception in thread "main" java.lang.RuntimeException:  java.lang.ClassNotFoundException: org.postgresql.Driver
at dao.ConnectionFactory.getConnection(ConnectionFactory.java:19)
at dao.CarroDAO.<init>(CarroDAO.java:21)
at teste.Teste.main(Teste.java:18)
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at dao.ConnectionFactory.getConnection(ConnectionFactory.java:16)

I inserted the jdbc postgres driver.

The Connectionfactory class:

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionFactory {

private String url = "jdbc:postgresql://localhost:5432/carro";
private String username = "postgres";
private String password = "";


public Connection getConnection(){
    try{
        Class.forName("org.postgresql.Driver");
        return DriverManager.getConnection(url,username,password);
    }catch(SQLException | ClassNotFoundException e){
        throw new RuntimeException(e);
    }
 }
}
  • 3

    You downloaded the postgre driver and added the jar to the classpath?

  • It would be nice if you implemented a unit test within the IDE. Better than creating a main.

  • @Diegofelipe, just a moment I’m seeing this.

  • @Diegofelipe did what you asked :). THANK YOU! It worked.

  • Failed to add the jar to the classpath.

1 answer

3


Lower the jar of this website, and add to the classpath of your project. The cause of the error is because the class org.postgresql.Driver was not located, probably because the driver jar was not added to the project’s classpath.

Another detail, if you’re using newer versions of jdk, such as 1.6 or newer (which has version 4 of jdbc), you don’t need to inform Class.forName, provided that the database driver jar is added to the project’s classpath, jdbc himself already automatically registers.

  • I was already with the driver, just needed to include in the classpath.

  • Now every project I need to add the jar to the classpath is this?

  • 2

    @Alinegonzaga Yes

  • 1

    @Alinegonzaga yes, all projects you need to connect to the database, you need to add the corresponding driver to this database in the classpath, so that io jdbc can identify it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.