Screen to configure Mysql connection

Asked

Viewed 284 times

1

I am currently using the following code to run connections with my DB

public class ConexaoDAO {
    public Connection getConexao() {
        try {
            return DriverManager.getConnection("jdbc:mysql://ip:porta/tabela", "usuario", "senha");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

But I would like to be able to configure the "ip" and "port" without having to change the code for this, because I want this program to be able to communicate with a DB on a non-local server (localhost).

I had thought of creating a file where I could put the data and as soon as I ran the program, it would read the information from that file to run the connection.

Any idea/tip on how to resolve this situation?

Note: All classes that communicate with DB use this class to make the connection.

Grateful for the attention.

  • You want to save this data part, is that it? To make small screen just for this I find a waste of time.

  • basically yes, like a configuration file. Any other suggestions? I had thought about setting a fixed ip for the server inside the network, but I don’t feel comfortable with this idea.

  • I was going to suggest a file .properties instead of canvas, but have answered about below.

2 answers

2


How about using a Factory pattern with getters and setters?

public class MySqlConnectionFactory {
    private String host;
    private int porta;
    private String database;
    private String usuario;
    private String senha;

    public MySqlConnectionFactory() {
    }

    public MySqlConnectionFactory(String host, int porta, String database, String usuario, String senha) {
        this.host = host;
        this.porta = porta;
        this.database = database;
        this.usuario = usuario;
        this.senha = senha;
    }
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPorta() {
        return porta;
    }

    public void setPorta(int porta) {
        this.porta = porta;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public Connection criarConexao() {
        if (host == null) throw new IllegalStateException("O host não foi definido.");
        if (porta == 0) throw new IllegalStateException("A porta não foi definida.");
        if (database == null) throw new IllegalStateException("O database não foi definido.");
        if (usuario == null) throw new IllegalStateException("O usuário não foi definido.");
        if (senha == null) throw new IllegalStateException("A senha não foi definida.");

        try {
            return DriverManager.getConnection("jdbc:mysql://" + host + ":" + porta + "/" + database, usuario, senha);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

Or if you prefer, use the Builder pattern:

public class MySqlConnectionBuilder {
    private final String host;
    private final int porta;
    private final String database;
    private final String usuario;
    private final String senha;

    public MySqlConnectionBuilder() {
        this(null, 0, null, null, null);
    }

    public MySqlConnectionBuilder(String host, int porta, String database, String usuario, String senha) {
        this.host = host;
        this.porta = porta;
        this.database = database;
        this.usuario = usuario;
        this.senha = senha;
    }

    public String getHost() {
        return host;
    }

    public MySqlConnectionBuilder withHost(String newHost) {
        return new MySqlConnectionBuilder(newHost, porta, database, usuario, senha);
    }

    public int getPorta() {
        return porta;
    }

    public MySqlConnectionBuilder withPorta(int novaPorta) {
        return new MySqlConnectionBuilder(host, novaPorta, database, usuario, senha);
    }

    public String getDatabase() {
        return database;
    }

    public MySqlConnectionBuilder withDatabase(String newDatabase) {
        return new MySqlConnectionBuilder(host, porta, newDatabase, usuario, senha);
    }

    public String getUsuario() {
        return usuario;
    }

    public MySqlConnectionBuilder withUsuario(String novoUsuario) {
        return new MySqlConnectionBuilder(host, porta, database, novoUsuario, senha);
    }

    public String getSenha() {
        return senha;
    }

    public MySqlConnectionBuilder withSenha(String novaSenha) {
        return new MySqlConnectionBuilder(host, porta, database, usuario, novaSenha);
    }

    public Connection criarConexao() {
        if (host == null) throw new IllegalStateException("O host não foi definido.");
        if (porta == 0) throw new IllegalStateException("A porta não foi definida.");
        if (database == null) throw new IllegalStateException("O database não foi definido.");
        if (usuario == null) throw new IllegalStateException("O usuário não foi definido.");
        if (senha == null) throw new IllegalStateException("A senha não foi definida.");

        try {
            return DriverManager.getConnection("jdbc:mysql://" + host + ":" + porta + "/" + database, usuario, senha);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
  • A builder getting all these properties would be fine huh =)

  • @diegofm I thought about it too, but then in this case, it would be easier to build the entire connection string at once.

  • @diegofm I changed the answer by putting a Builder too.

  • This Builder became more advanced but much more interesting with these options to change only certain parameters instead of all of the connection.

1

You can leave all your configuration in a file application.properties, commonly placed in your Resources folder (thinking of the Maven project structure) and read its settings. This even allows you to have multiple files according to your environment. For example: dev.application.properties and prod.application.properties. It also has the advantage of allowing versioning of this configuration.

The other option is to put this information in the environment variables. I don’t like this approach especially if you have to put the passwords there, and you would have more work to ensure that everything is properly configured in each brand, but anyway it’s a possibility.

Browser other questions tagged

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