-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();
}
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.
– dougg0k
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!
– Júnior Nascimento