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);
}
}
}
You downloaded the postgre driver and added the jar to the classpath?
– user28595
It would be nice if you implemented a unit test within the IDE. Better than creating a main.
– josivan
@Diegofelipe, just a moment I’m seeing this.
– Aline
@Diegofelipe did what you asked :). THANK YOU! It worked.
– Aline
Failed to add the jar to the classpath.
– Aline