'Cant connect Java to MariaDB

I cant connect java to MariaDB .I saw all similar questions answers but none has solved my problem. Here is my code :

/** To change this license header, choose License Headers in Project
 *   Properties.  * To change this template file, choose Tools | Templates 
 * and open the template in the editor.  
 */
package mariadbtest; 

import java.sql.*;


/**
 * 
 * @author AAAA  
 */ 
public class MariaDBTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){  
        try {
            Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
            String url = "jdbc:MariaDB://localhost:3306/customer"; // url to access the DB
            String user = "root";
            String pwd="password";
     
            Connection connection = DriverManager.getConnection(url,user,pwd);
     
            System.out.println("Connected to MariaDB");
        } 
        catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString()); 
        } 
        catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString()); 
        }
        catch (Exception ex) {
            System.out.println("Exception: "+ ex.toString()); 
        }

        // TODO code application logic here
    }
}

Note the following :

  • I am using the connector mysql-connector-java-8.0.22.
  • I have the latest java version.
  • MariaDB version is 10.3.27.
  • I have copied the connector to the lib directory in java folder in program files .
  • I have added the connector JAR file to my project through properties --> Libraries --> Add JAR/Folder.
  • MariaDB has the service name MariaDB.
  • The database name is customer.

error : SQL Exception: java.sql.SQLException: No suitable driver found for jdbc:MariaDB://localhost:3306/customer BUILD SUCCESSFUL (total time: 3 seconds)



Solution 1:[1]

I think that the problem is that you are using a MySQL Connector/J JDBC driver with a URL that it doesn't recognize. This URL

    jdbc:MariaDB://localhost:3306/customer

says to look for a driver from a "MariaDB" provider. But apparently the MySQL drivers don't advertise themselves (via SPI) as being MariaDB drivers.

Solution

First of you should get rid of this:

Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

It is not needed, and you are hard-wiring a driver classname into your code. Get rid of the associated exception handler clauses too. (This is all Voodoo Programming stuff. It might have been needed 20 years ago ... but not anymore.)

You can fix the driver resolution problem in two ways:

  1. Change the URL to this:

    jdbc:mysql://localhost:3306/customer
    

    According to the MariaDB documentation, the MySQL Connector/J driver is compatible with MariaDB. But use of "MariaDB" in the JDBC url not mentioned in the MySQL Connector/J documentation.

  2. Switch to the MariaDB Connector/J driver. According to the documentation, this should support

    jdbc:mariadb://localhost:3306/customer
    

    or

    jdbc:mysql://localhost:3306/customer
    

It is not clear from the docs whether the case of the "protocol" part matters. However, syntax specification in the MariaDB doc uses "mariadb" not "MariaDB", so I would recommend lower-case for stylistic consistency.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1