java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase

1.8k Views Asked by At

I have mariadb 10.4 running on my system. The db connection is good, i have tested with db workbench with the root password. On the other hand, I try to connect to it from my Java code, i have the exception below:

java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase

My java code is as below:

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

public class CountryDAO {


    public void list() throws SQLException{
        String databaseURL = "jdbc:mariadb://localhost:3306/mydatabase";
        String user = "root";
        String password = "root";

        try 
        {
            Connection connection = DriverManager.getConnection(databaseURL, user, password);

        } catch (SQLException ex) {
            ex.printStackTrace();
            throw ex;
        }      


    }
}

I am calling this code from a servlet. I am using eclipse and i have added the mariadb-java-client-2.4.2.jar as external jar from build path. It should be working but it doesn't.

UPDATE: Firstly, I have tested my code in a new Eclipse Java project and it works like a charm. Secondly, i have tried to call the same method from a main method of another class, the db connection also work perfectly. So i guess my problem is because i call this connection from a Servlet. I don't know why it does so but i am kind of stuck. Do you have any suggestions?

1

There are 1 best solutions below

4
On

You have to load the driver first.

Class.forName("org.mariadb.jdbc.Driver");