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);
}
}
}