【问题标题】:Groovy: RedHat: java.sql.SQLException: No suitable driver found for jdbc:mysql:Groovy:RedHat:java.sql.SQLException:找不到适合 jdbc:mysql 的驱动程序:
【发布时间】:2014-05-14 12:49:59
【问题描述】:

我尝试在 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

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

【问题讨论】:

  • 您可以尝试从/opt/groovy/lib 中删除它吗?在同一个类路径上有两个相似的 jar 绝不是一件好事
  • 网址是不是有错误?不应该是:jdbc:mysql://10.242.182.152:3306/<DB_NAME>DB_NAME 也不见了?
  • @Opal:在我的代码中,我使用这样的语法:sql.eachRow("SELECT * FROM "+name+".TABLE WHERE COLUMN = 'VALUE') name 是数据库的名称。我的程序在 Windows 下运行,但在 RH 下不运行。
  • @tim_yates:我从 /opt/groovy/lib 目录中删除了 mysql-connector-java-5.1.25-bin.jar 以及何时我执行我的脚本它在控制台中冻结.. 我没有看到我的脚本的工作结果,并且中断我的脚本执行的唯一方法是通过 CTL+C 打破它

标签: mysql jdbc groovy driver redhat


【解决方案1】:

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/

希望这会有所帮助,

【讨论】:

  • 执行后仍然冻结
  • 是的,我有。我尝试删除此文件并再次添加。它没有帮助。
猜你喜欢
  • 2016-11-28
  • 2015-10-02
  • 2014-08-05
  • 2018-12-13
  • 2015-09-22
  • 2019-10-16
  • 2016-10-01
  • 2012-05-08
  • 2017-08-26
相关资源
最近更新 更多