Screen to configure connection with Mysql

1

I am currently using the following code to execute 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 "ip" and "port" without needing to change the code for this, since I want this program to communicate with a DB on a non localhost server.

I had thought about 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 execute the connection.

Any ideas / tips on how to resolve this situation?

Note: all classes that communicate with the DB use this class to connect.

Thank you for your attention.

    
asked by anonymous 22.11.2016 / 14:58

2 answers

2

How about using a factory default 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 default builder:

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);
        }
    }
}
    
22.11.2016 / 15:10
1

You can leave all of your configuration in a application.properties file, commonly placed in your resources folder (thinking about 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 do not like this approach especially if you have to put the passwords there, and you would have more work to make sure everything is correctly set up on each machine, but anyway it's a possibility.

    
22.11.2016 / 15:10