MYSQL No suitable driver found

2

I currently use Intellij, it turns out that I'm trying to connect to a MySQL database, but after adding the library and using a class connection (which works perfectly with the MariaDB driver) I find the error in the title of the question

public void conectar() {
        try {
            conexion = DriverManager.getConnection(url, usuario, contraseña);
            if (conexion != null) { JOptionPane.showMessageDialog(null, "Conexión establecida a : \n" + url,  "ACDA2", JOptionPane.INFORMATION_MESSAGE);
                Class.forName("com.mysql.jdbc.Driver");
                stm = conexion.createStatement();//crea un objeto que permite enviar instrucciones a la base de datos
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Conexión fallida a : \n " + url, "", JOptionPane.ERROR_MESSAGE);
            System.out.println(ex.getMessage());
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Error al cargar el driver", "", JOptionPane.ERROR_MESSAGE);
            System.out.println(e.getMessage());
        }
    }

These are the values that I send to my class connection

   c= new conexion("jdbc:mysql://", "127.0.0.1/", "root", "", "sanciones");
   c.conectar();

And then I detail the constructor of the connection class

 public conexion(String driver,String host, String usuario, String 
 contraseña, String baseDatos) {
    this.usuario = usuario;
    this.contraseña = contraseña;
    this.baseDatos = baseDatos;
    this.driver = driver;
    this.host = host;
    this.url = driver + this.host  + this.baseDatos;
}

STATE UPDATE

I have managed to connect to MySQL by placing the Class.forName before connecting

After reading the MySQL manuals the following question arises, from the connector 4.0 it is not necessary to use the Class.ForName to load the library since it would automate the process, then .... Why does it work if I place that line? before connecting and then not? I have tried to create a project in which only connects with a single line (Without using Class.ForName), and does not connect (No driver found) I remind you that I use java 1.8 and the 5.1.5 connector, I have also tried the connection in NetBeans and Eclipse and it also does not work without the Class.ForName

SOLUTION

The driver versions com.mysql.jdbc_5.1.5 do not allow the implicit loading of the driver due to the lack of the Services subfolder in META-INF and its corresponding content, even if it is JDBC4 and in the mysql manuals it is said that it is possible, it is not, at least with this specific version, greetings

    
asked by Rafael Valls 10.11.2018 в 13:40
source

1 answer

1

Check that within your folder META-INF of the driver is a folder called services which must contain a document that has com.mysql.jdbc.Driver if you do not have this folder of services would be problem of the driver that you have downloaded , since the ClassLoader does not load the class automatically and you must use the Class.ForName() to load it explicitly.

    
answered by 12.11.2018 / 10:31
source