How to fix "Sqlexception: No suitable driver found" error while making a connection?

Asked

Viewed 8,737 times

2

I am trying to make the connection to the database in my code, however it appears the following error:

Exception in thread "main" java.sql.Sqlexception:
No suitable driver found for jdbc:mysql://localhost:3306/usuario

Code:

public class ConexaoBd {
    public static void main(String[] args) throws SQLException{
        Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/usuario","root","tonhaoroot");

        conexao.close();    
    }
}

1 answer

6


Before creating the connection you need to register the mysql driver. It will look like this:

public class ConexaoBd {
    public static void main(String[] args) throws SQLException{
        Class.forName("com.mysql.jdbc.Driver"); /* Aqui registra */
        Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/usuario","root","tonhaoroot");

        conexao.close();    
    }
}

Also check that the mysql(.jar) driver is in the application’s classpath.

  • Hummm, I really thought it had something to do with mysql drive. I’m going to test it here man. I’m warning you if it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.