Return Connection as Boolean?

Asked

Viewed 146 times

2

It would be possible to modify the class so that it returns to jFrame the result of the connection as Boolean?

public class ConectarDB {

    private static Connection con = null;

    public static Connection getConexao() {

        // Drive do PostGreSQL
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException ex) {
           ex.printStackTrace();
           return null;
        }

        // Variáveis de Conexão
        String drive    = "jdbc:postgresql";
        String ip   = "localhost";
        String port     = "5432";
        String db   = "basedados";
        String user     = "postgres";
        String password = "senha";
        String conexao  = drive + "://" + ip + ":" + port + "/" + db;

        // Conectar-se ao Banco de Dados
        try {
           con = DriverManager.getConnection(conexao, user, password);        
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível se conectar ao Banco de Dados!", "Aviso!", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
            return null;
        }

        // Retorno da Informação
        return con;

    }

    public void main(String[] args) {
        getConexao();
    }

}

Return to validate

if(netResultado.equals(false)) {
                    JOptionPane.showMessageDialog(null, "Sem conexão com a Internet.", "Informação", JOptionPane.WARNING_MESSAGE);
                } else if(dbResultado.equals(false)) {
                    JOptionPane.showMessageDialog(null, "Sem conexão com o Banco de Dadost.", "Informação", JOptionPane.WARNING_MESSAGE);
                } else if(netResultado.equals(true)) {
  • 1

    boolean testeConexao = con.isValid(5);

  • Hello, Eduardo. Where can I put this command line?

1 answer

1


Try this

public class Conectardb {

private static Connection con = null;

public static boolean getConexao() {

    // Drive do PostGreSQL
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException ex) {
       ex.printStackTrace();
       return null;
    }

    // Variáveis de Conexão
    String drive    = "jdbc:postgresql";
    String ip   = "localhost";
    String port     = "5432";
    String db   = "basedados";
    String user     = "postgres";
    String password = "senha";
    String conexao  = drive + "://" + ip + ":" + port + "/" + db;

    // Conectar-se ao Banco de Dados
    try {
       con = DriverManager.getConnection(conexao, user, password);
       return true;
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Não foi possível se conectar ao Banco de Dados!", "Aviso!", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
        return false;
    }

    // Retorno da Informação


}

public static void main(String[] args) {
    getConexao();
}

}

  • It works. It just won’t work for what I need. Thank you very much!!

  • 1

    You need exactly what for? If I can talk I can help.

  • If the connection has given error when returning to the main Jframe it informs that there has been an error. I didn’t want to leave the error message in this window. If I change to Boolean affects a lot of things in my project. Thank you!! NOTE: Code added at the beginning of the page.

  • 1

    I don’t quite understand yet, but if your doubt is to display the error only there in that second snippet of code that you put, I suggest using the keyword throws in the getConexao() statement, throws basically says that you should capture a possible exception in the method call and thus you are not required to capture the error in the method implementation. [http://answall.com/questions/17025/usando-as-palavras-chave-throws-e-throw]

  • I had no error regarding the code but I have classes that need the connection and is giving error because now it is Boolean understand? More no problem. Thank you!

Browser other questions tagged

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