java.sqlexception: no suitable driver found for root


I created a java program that creates a connection to the database and I get the error java.sqlexception:no suitable driver found for root .

I do not know where the error can come from and I need help.

I show you my code below:

package principal;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.logging.Level;

import java.util.logging.Logger;

public class conexion {

    private String cadenaconecion="jdbc:mysql://";

    private String usuario ="root";

    private String contrasena="";

    private Connection con =null;

    public Connection getConnection() throws SQLException 

            try {
               con= (Connection) DriverManager.getConnection(usuario, cadenaconecion,contrasena);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null,ex);

            return con;
asked by Agustin Hernandez Luna 25.11.2018 в 08:19

1 answer


It seems that you are omitting or missing the port in your connection string, in the following example the port is 3306

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String connection = "jdbc:mysql://localhost:3306/YourDBName";
    String user = "root";
    String password = "root";
    Connection con = DriverManager.getConnection(connection, user, password);
    if (!con.isClosed()) {
answered by 25.11.2018 в 10:36