Reputation: 111
I currently use Intellij, it turns out that I am trying to connect to a MySQL database, but after adding the library and using a connection class which works perfectly with the MariaDB driver, I find the error in the question title
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 connection class constructor
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;
}
UPDATE
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
Upvotes: 0
Views: 158
Reputation: 590
"Class.forName" need to be called first to load the proper driver first.Try this,
try {
Class.forName("com.mysql.jdbc.Driver");
conexion = DriverManager.getConnection(url, usuario, contraseña);
if (conexion != null) { JOptionPane.showMessageDialog(null, "Conexión establecida a : \n" + url, "ACDA2", JOptionPane.INFORMATION_MESSAGE);
stm = conexion.createStatement();//crea un objeto que permite enviar instrucciones a la base de datos
}
Upvotes: 0