Groovy:RedHat:java.sql.SQLException:没有为jdbc:mysql找到合适的驱动程序:

时间:2014-05-14 12:50:00

标签: mysql jdbc groovy driver redhat

我尝试在RH shell

下执行groovy-script
  

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

但收到异常

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)

我安装了下一个包:

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

和我的代码:

@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)

我的groovy( / opt / groovy / lib )目录中还有 mysql-connector-java-5.1.25-bin.jar

请提供解决错误异常的解决方案

1 个答案:

答案 0 :(得分:0)

当你的网址/正在寻找com.mysql.jdbc.Driver时,你的连接网址中缺少jdbc:mysql://个字符:

com.mysql.jdbc.Driver上课:

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上课:

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;
        }
            ...
 }

因此:

更改:

jdbc:mysql:/localhost:3306/

jdbc:mysql://localhost:3306/

希望这有帮助,