Java - How do I leave the Postgresql Permanent Connection IP?

Asked

Viewed 439 times

-1

Good morning,

Every time my machine connects to the internet, her IP address changes. My question is... How do I always get access to Postgresql even if the IP of my machine changes?

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";                // Drive do PostGreSQL
    String ip       = "localhost";                      // IP de Conexão ao Servidor
    String port     = "5432";                           // Porta do PostGreSQL
    String db       = "database_1";                     // Nome do Banco de Dados no PostGreSQL
    String user     = "postgres";                       // Nome do Usuário no PostGreSQL
    String password = "postgres";                       // Senha do Usuário no PostGreSQL
    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;

}

// Main
public void main(String[] args) {
    getConexao();
}
  • 1

    Junior, do you want your machine to be a database server for an outside application to access? Because if it’s just local, localhost is the answer, because even if your external ip changes, it won’t influence.

  • Well, for how much can the server be yes for others to access. More how can I do this? Since the class will look for who is localhost... and will see that there is nothing on that PC. Could you instruct me? Thank you!

1 answer

1

Usually the database server is on its own server in the web cloud, so the datacenter servers have static and non-dynamic ip as the connections we make directly from home.

And many of these servers, even on the web, are not directly spouses, often for security reasons, are only released on site.

But then, how can we make the connection to this database?

Generalizing often there is one web application or web service running together on that server, on that same datacenter, thus being possible a local connection.

In case you asked here, if your desktop application is going to access your local database, localhost is the correct one, but if it is going to distribute to other users outside your internal network, you will need to have a web server to have a static/fixed ip.

If you want your machine to be a database server even with dynamic ip, use a service such as no-ip to free a host by masking its ip, and each time it changes its ip, the no-ip application updates the host with its new ip.

This is just an alternative, not very recommended for reasons.

Having a host or fixed ip, make use of the configuration instead of localhost, so everyone you have with the application will access the same database.

Browser other questions tagged

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