java.lang.RuntimeException: java.lang.ClassNotFoundException: org.postgresql.Driver

0

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

In the test class of the TEST package it looks 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); 
 }  
}

But when I hedge it is giving 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);
    }
 }
}
    
asked by anonymous 16.02.2016 / 13:56

1 answer

3

Download the jar from this site , and add it to the classpath of your project. The cause of the error is because the org.postgresql.Driver class was not found, probably because the driver jar was not added to the project classpath.

Another detail, if you are using newer versions of jdk, such as 1.6 or newer (which has version 4 of jdbc), you do not need to enter Class.forName , as long as the bank driver jar is added in the project classpath , jdbc itself is already automatically registered . / p>     

16.02.2016 / 14:03