17

I am trying to connect to a database in Mariadb through a simple java application but the connection is told to be unsuccessful and an Exception is thrown. I have done the similar connection using mysql and it was working correctly. The problem is maybe with the driver here.

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

        Connection connection = DriverManager.getConnection(  
                "jdbc:mariadb://localhost:3306/project", "root", "");  
        Statement statement = connection.createStatement(); 

        String uname="xyz",pass="abc";
       statement.executeUpdate("insert into user values('"+uname+"','"+pass+"')");}//end of try block

I looked up the internet for the help and came by that driver class provided by the MariaDB Client Library for Java Applications is not com.mysql.jdbc.Driver but org.mariadb.jdbc.Driver! I changed it accordingly but it seems the problem is with the very first line inside the try block. The driver is not loading at all. Also, I have added the mysql jar file to the libraries of my java application as in the screen-shot below. Please help me through this.enter image description here

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Nitish Pareek
  • 2,453
  • 3
  • 19
  • 18

2 Answers2

22

It appears that you are trying to use jdbc:mariadb://... to establish a connection to a MariaDB server instance using the MySQL JDBC Driver. That probably won't work because the MySQL JDBC Driver would use jdbc:mysql://..., regardless of whether it is connecting to a MySQL server or a MariaDB server. That is, the connection string must match the driver that is being used (rather than the database server being accessed).

The MySQL and MariaDB drivers are supposed to be somewhat interchangeable, but it only seems prudent to use the MariaDB connector when accessing a MariaDB server. For what it's worth, the combination of mariadb-java-client-1.1.7.jar

projectProperties.png

and

Connection con = DriverManager.getConnection(
        "jdbc:mariadb://localhost/project", 
        "root", 
        "whatever");

worked for me. I downloaded the MariaDB Client Library for Java from here:

https://downloads.mariadb.org/client-java/1.1.7/

which I arrived at via

https://downloads.mariadb.org/

Additional notes:

  1. There is no need for a Class.forName() statement in your Java code.

  2. The default configuration for MariaDB under Mageia may include the skip-networking directive in /etc/my.cnf. You will need to remove (or comment out) that directive if you want to connect to the database via JDBC because JDBC connections always look like "network" connections to MySQL/MariaDB, even if they are connections from localhost. (You may need to tweak the bind-address value to something like 0.0.0.0 as well.)

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Did the changes and then only after a restart, the things worked. Merely commenting out the skip-networking directive in /etc/my.cnf file worked along with adding the library to the project properties. – Nitish Pareek Apr 28 '14 at 06:26
  • But the problem persists when with the Java Web Application. There it shows that the driver is missing. – Nitish Pareek May 02 '14 at 17:05
  • @NitishPareek A quick search revealed several answers like [this one](http://stackoverflow.com/a/2862260/2144390). Does that help? – Gord Thompson May 02 '14 at 17:30
  • Thanks for your help but this is not what I encountered. In a simple java application your previous solution worked but with a Java web application, after we add the jar file there is a check box that represents some "Package" besides. I can't find the solution to this one. – Nitish Pareek May 02 '14 at 17:49
  • This might be unrelated, but when trying to add my database to Netbeans using the mariadb-driver, I actually had to use mysql: instead of mariadb: (e.g. jdbc:mysql://my_host:3306/database). Otherwise I got the message the jdbc url was invalid. Any ideas as to why that is? – Hans Wouters Jul 24 '15 at 08:58
13

An additional note: Exploring the MariaDB JDBC driver, I found this inside the url parsing file:

Project: https://github.com/MariaDB/mariadb-connector-j.git
File: src/main/java/org/mariadb/jdbc/UrlParser.java

public static UrlParser parse(final String url, Properties prop) throws SQLException {
....
        if (url.startsWith("jdbc:mysql:")) {
            UrlParser urlParser = new UrlParser();
            parseInternal(urlParser, url, prop);
            return urlParser;
        } else {
            if (url.startsWith("jdbc:mariadb:")) {
                UrlParser urlParser = new UrlParser();
                parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
                return urlParser;
            }
        }    

As you can see, the string "jdbc:mariadb:" is always replaced with "jdbc:mysql:" internally. So when it comes to the MariaDB driver, whether it is :mariadb: or :mysql: it always gets parsed as "jdbc:mysql:".

No difference.

if (url.startsWith("jdbc:mariadb:")) {
    ....
    parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
    ....
Basil Musa
  • 8,198
  • 6
  • 64
  • 63
  • Great tip! Thanks. – Marco Jul 11 '16 at 10:26
  • 1
    That mysql and mariadb are both valid for the connection string is also stated in the MariaDB driver documentation as `jdbc:(mysql|mariadb):[replication:|failover:|sequential:|aurora:]//[,...]/[database][?=[&=]]` see also https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/#connection-strings – kap Jan 20 '17 at 13:12