1

I'm having problems connecting to the database from my plugin, previously I had done it and it left me normal, but from this other project it doesn't let me and I get errors, now this one is appearing and I don't understand

ConnectionMySQL.java

public class ConnectionMySQL {
    
    private Connection connection;

    public ConnectionMySQL(String host, int puerto, String database, String usuario, String password) {
        try {
            if (connection != null && !connection.isClosed()) {
                Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&4Error al conectar a la base de datos."));
                return;
            }
            Class.forName("com.mysql.jdbc.Driver");
            this.connection = DriverManager.getConnection("jdbc:mysql://"+host+":"+puerto+"/"+database,usuario,password);
            Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', "&aConectado a la base de datos correctamente."));
            
        } catch(SQLException | ClassNotFoundException e) {
            throw new RuntimeException("Hubo una excepción al conectar a la base de datos: ", e);   
        }
    }

    public Connection getConnection() {
        return connection;
    }
}

Main.java (onEnable)

private ConnectionMySQL connection;

public void onEnable() {
    
    FileConfiguration config = getConfig();

    // PATHS CONFIGS


    String host = "mysql.host";
    String port = "mysql.port";
    String database = "mysql.database";
    String usuario = "mysql.username";
    String password = "mysql.password";
    this.connection = new ConnectionMySQL(config.getString(host),
    config.getInt(port),
    config.getString(database),
    config.getString(usuario),
    config.getString(password));  

    // REGISTRO DE COMANDOS Y CONSTRUCTOR JDA
    registerCommands();

    Main.instance = this;
    
    System.out.println("zDiscord plugin is online");
    saveDefaultConfig();
}

The full error is this: https://pastebin.com/Qq9r8XTv

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
Abel Albor
  • 11
  • 1

1 Answers1

-1

you can try as below:

 Connection connection = DriverManager.getConnection("jdbc:mysql://"+host+":"+puerto+"/"+database,usuario,password);

instead of this.connection=DriverManager.getConnection("jdbc:mysql://"+host+":"+puerto+"/"+database,usuario,password);

ritu mansata
  • 77
  • 2
  • 13
  • it was nothing in the code, I was entering the connection data wrong, add the port in quotes in the configuration file, anyway thank you very much – Abel Albor Jul 30 '22 at 21:40