lsv
lsv

Reputation: 1757

Groovy: RedHat: java.sql.SQLException: No suitable driver found for jdbc:mysql:

I tryed to execute groovy-script under RH shell

[localhost]# groovy /home/rualas4/script.groovy

but received exception

Caught: java.sql.SQLException: No suitable driver found for jdbc:mysql:/localhost:3306/
java.sql.SQLException: No suitable driver found for jdbc:mysql:/localhost:3306/
        at script.run(script.groovy:13)

I had installed next packages:

unixODBC-2.2.14-12.el6_3.x86_64
mysql-connector-odbc-5.3.2-1.el6.x86_64

and my code:

@GrabConfig(systemClassLoader = true)
@Grab(group='mysql', module='mysql-connector-java', version='5.1.25')

import groovy.sql.Sql
import groovy.io.FileType

println "Initialize connection"
url="jdbc:mysql://localhost:3306/"
username = "test"
password = "test"
driver = "com.mysql.jdbc.Driver"
sql = Sql.newInstance(url, username, password, driver)

also I have mysql-connector-java-5.1.25-bin.jar in my groovy (/opt/groovy/lib) directory

Please, provide a solution for resolve error exception

Upvotes: 0

Views: 1622

Answers (1)

albciff
albciff

Reputation: 18507

You are missing a / character in your connection url, when com.mysql.jdbc.Driver parse your url is looking for jdbc:mysql://:

com.mysql.jdbc.Driver class:

package com.mysql.jdbc;

import java.sql.SQLException;

  public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    // ~ Static fields/initializers
    // ---------------------------------------------

    //
    // Register ourselves with the DriverManager
    //
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }

    // ~ Constructors
    // -----------------------------------------------------------

    /**
     * Construct a new driver and register it with DriverManager
     * 
     * @throws SQLException
     *             if a database error occurs.
     */
    public Driver() throws SQLException {
        // Required for Class.forName().newInstance()
    }
  }

com.mysql.jdbc.NonRegisteringDriver class:

package com.mysql.jdbc;

import java.sql.SQLException;

public class NonRegisteringDriver implements java.sql.Driver {

    ...

    private static final String URL_PREFIX = "jdbc:mysql://";

    ...
public Properties parseURL(String url, Properties defaults)
            throws java.sql.SQLException {
        Properties urlProps = (defaults != null) ? new Properties(defaults)
                : new Properties();

        if (url == null) {
            return null;
        }

        if (!StringUtils.startsWithIgnoreCase(url, URL_PREFIX)
                && !StringUtils.startsWithIgnoreCase(url, MXJ_URL_PREFIX)
                && !StringUtils.startsWithIgnoreCase(url,
                        LOADBALANCE_URL_PREFIX)
                && !StringUtils.startsWithIgnoreCase(url,
                        REPLICATION_URL_PREFIX)) {

            return null;
        }
            ...
 }

therefore:

Change:

jdbc:mysql:/localhost:3306/

To:

jdbc:mysql://localhost:3306/

Hope this helps,

Upvotes: 1

Related Questions