Why can’t I make the connection to Postgresql?

Asked

Viewed 577 times

0

I’m very interested in programming but I’m new in the area and I need to give this work to the teacher. But I can’t solve this mistake.

package dal;
import java.sql.*;
import javax.swing.JOptionPane;
public class ConectaBd {
    public static Connection conectabd() throws ClassNotFoundException, SQLException{

        try{
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432","postgres","123");
            JOptionPane.showMessageDialog(null, "Conecxão bem sucedida !");
            return con;
        }

        catch(SQLException error){
            JOptionPane.showMessageDialog(null, "Não foi possível conectar !");
            return null;    
        }
    }
}

That is the mistake:

mai 22, 2017 10:19:47 pm org.postgresql.Driver connect GRAVE: Error in url: jdbc:postgresql://localhost:5432 HALTED CONSTRUCTION (total time: 3 minutes 23 seconds)

  • 3

    Apparently the database name is missing from the connection url jdbc:postgresql://localhost:5432/suaDataBase in getConnection

  • the user @Brow-joe is correct, https://jdbc.postgresql.org/documentation/80/connect.html

1 answer

1

The database name is missing.

public static Connection getConnection() throws SQLException{
        try {
             Class.forName("com.mysql.jdbc.Driver");                             
             return DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx/mybd?user=eu&password=eu");                         
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());            
            throw new SQLException();            
        }        
    } 
}

Browser other questions tagged

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