Error "No suitable driver found for jdbc: mysql: // localhost / dda" when connecting to MySql from Java

2

I would like to connect to a database that I have locally. I have the service up, and I already have the base created.

as shown is localhost, root pass 1234,

and I have 2 user and oid tables,

I have the following Java class to use this base

 private Persistencia() {
        base = BaseDatos.getInstancia();
        base.conectar("jdbc:mysql://localhost/dda", "root", "1234");
    }


 public void conectar(String url,String u,String p){
        try {
            conexion = DriverManager.getConnection(url, u, p);
            stmt = conexion.createStatement();
        } catch (SQLException ex) {
            System.out.println("Error al conectar:" + ex.getMessage());
        }
    }

The error I have is the following:

  

Error when connecting: Not suitable driver found for jdbc: mysql: // localhost / dda

    
asked by Bruno Sosa Fast Tag 02.12.2017 в 22:09
source

3 answers

3

I add this answer, because in fact the accepted answer is incorrect.

When one receives the error:

  

No suitable driver found for jdbc: mysql: ...

... this means that the JDBC code is unable to find the necessary driver to establish the connection to the MySQL database.

With modern Java versions, there is only one explanation for this error: that the MySQL .jar connector is not in the application classpath.

In this case, the solution is to simply download the connector .jar from the MySQL site and add it to the classpath of the application.

Help adding a call to Class.forName("com.mysql.jdbc.Driver") ?

No, it does not change anything.

In much older versions of Java / JDBC, the error could also happen even if the .jar was in the classpath. And in these cases, it was indeed recommended to use Class.forName("com.mysql.jdbc.Driver") to make sure that the class Driver was loaded before trying to establish the connection.

But from Java 6 / JDBC 4.0 (more than 10 years ago!), changes were included so that it was no longer necessary to call Class.forName . Note what the DriverManager documentation for Java 6 says ( and of course, the documentation says the same for later versions of Java):

  

Applications no longer need to explictly load JDBC drivers using Class.forName() .

Free translation:

  

Applications no longer need to load the JDBC drivers explicitly using Class.forName() .

Since this adjustment was made more than 10 years ago, it would be good if we did not continue spreading this obsolete idea that should be called Class.forName in these situations.

    
answered by 03.12.2017 / 01:44
source
1

You tried to load the Driver Class.forName("com.mysql.jdbc.Driver");

public Connection conexion() {
    if (conexion == null) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conexion= DriverManager.getConnection("jdbc:mysql://"+host+"/"+bd, usuario, contrasena);
            System.out.println("exito");
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(conexionBD.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("error");
        }

    }
    return conexion;
}
    
answered by 02.12.2017 в 22:24
1
  • Initially you must download the java connector for the specific database and add the library to your project.
  •   

    Right click on your project, then select Properties and Categories select Libraries and on your right click on Add Library , finally you select the path where your downloaded connector is stored.

  • then you can connect to the database by loading the driver and generating a connection and then send the SQL statement.

    public Connection getConexion () {

    Connection conexion = null;
        try {
                Class.forName("com.mysql.jdbc.Driver");  
                if(this.getDireccion().equalsIgnoreCase("localhost")){//se conecta en modo local
                    url = "jdbc:mysql://localhost"; //personal
                    conexion = DriverManager.getConnection(url,"root","clave");
                }else{//se conecta en modo remoto
                    url="jdbc:mysql://"+this.getDireccion(); 
                    conexion = DriverManager.getConnection(url,"usuarioRemoto","clave");
                }
            } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
                e.printStackTrace();
                    JOptionPane.showMessageDialog(
                        new JFrame(),e.getLocalizedMessage()+
                        "Base de Datos no encontrada",
                        "ADVERTENCIA",
                        JOptionPane.INFORMATION_MESSAGE);
                System.out.println(e.getLocalizedMessage()+"Base de Datos no encontrada");
    

    }

    return conexion;
    

    }

  • answered by 03.12.2017 в 02:34