1
I’m using Netbeans and can’t establish a connection. I added . postgre jar (postgresql-42.2.5.jar) in the lib folder and tested the connection in the Netbeans tab (Service > database), ok worked. but I keep falling in the catch block Follows the code:
package teste.de.conexão;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConexaoJDBC {
String caminho = "jdbc:postgresql://localhost:5432/postgres";
String user = "postgres";
String password = "postgres";
public Connection getConnection() throws SQLException{
Connection con = null;
con = DriverManager.getConnection(caminho, user, password);
System.out.println("Conexão aberta com sucesso!! :)");
return con;
}
Class co main method:
package teste.de.conexão;
import java.sql.Connection;
import java.sql.SQLException;
public class TesteDeConexão {
public static void main(String[] args) {
ConexaoJDBC JDBC = new ConexaoJDBC();
try {
Connection conexao = JDBC.getConnection();
if(conexao != null){
System.out.println("Existe uma conexao");
conexao.close();
}
} catch (SQLException ex){
System.out.println("Erro");
ex.getMessage();
}
}
Exit:
Erro
BUILD SUCCESSFUL (total time: 1 second)
Why do I keep falling in the catch? I should add something else for Netbeans to find the . postgres jar?
Do me a favor? Where it says
System.out.println("Erro");
modify bySystem.out.println("Erro: " + ex.getMessage());
and display the error message.– Augusto Vasques
It was a problem with the authentication password, I also noticed that I was not printing the return of ex.getMessage(), so it was not displayed on the output. Thank you
– Lucas Marinzeck