Groovy 控制台/jdbc 驱动程序问题的类路径是什么? [英] What is classpath for Groovy Console / jdbc driver prblem?

查看:14
本文介绍了Groovy 控制台/jdbc 驱动程序问题的类路径是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的数据库代码在Java环境下是可以的(没有绑定值'c'),但是在Groovy控制台不能正常使用jdbc,异常

Such database code is OK in Java environment (without binded value 'c'), but in Groovy console can't properly use jdbc, with exception

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=

驱动程序类以脚本而闻名(加载时没有空值等)但可能未在 Drivermanager 中注册?

Driver class is known for script (is loaded without null etc) but probably not registered in Drivermanager ?

代码(我尝试使用和不使用 Class.forname() )

code (i try with and without Class.forname() )

import groovy.sql.Sql
import groovy.sql.DataSet

c =   Class.forName('com.microsoft.sqlserver.jdbc.SQLServerDriver')

def db = [url:'jdbc:sqlserver://localhost;databaseName=... ,driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
 def sql = Sql.newInstance(db )

我已经检查过的:

  1. 新启动的 Groovy 控制台在类路径上没有 sqljdbc4.jar.

自然异常 java.lang.ClassNotFoundException 在第 4 行 Class.forName(),或者如果第 4 行注释并且参数 db 带有驱动程序名称,则第 7 行的异常 <代码>Sql.newInstance(db)

Natural exception java.lang.ClassNotFoundException on line 4 Class.forName(), or if line 4 commented and parameters db with driver name the exception on line 7 Sql.newInstance(db )

它的逻辑,找不到驱动程序类等......

Its logical, driver class not found etc ...

1a.如果 db 参数有 3 个参数(没有驱动程序),我假设它的合法(并在其他情况下工作)异常更改为 SQLException: No合适的驱动程序 on line 7 Sql.newInstance(db )

1a. if db parameters with 3 arguments (without driver), I assume its legal (and working in other situations) exception changes to SQLException: No suitable driver on line 7 Sql.newInstance(db )

这也是合乎逻辑的,DriverManager 不知道如何解析 key jdbc:sqlserver .驱动程序没有注册,DriverManager 没有神奇的知识实现什么类.

It is logical too, DriverManager don't know how to resolve key jdbc:sqlserver . Driver isn't registered and DriverManager have not magical knowledge what class implements.

2.当我将 jar 添加到控制台类路径(脚本/将 jar(s) 添加到类路径)时,情况有所改变.不再有 ClassNotFoundException 和变量 c 具有非空值(驱动程序类),但 SQLException:没有合适的驱动程序 继续.

2. when I add jar to console classpath (Script / Add jar(s) to classpath) things are somewhat changed. No more ClassNotFoundException and variable c has non-null value (driver class) but SQLException: No suitable driver continues.

我对 JDBC 哲学的理解:(现代)JAR 驱动程序使用技术与文件 META-INF/services/java.sql.DriverDriverManager 闻名.所以在正确的情况下,第四个参数(驱动程序类名)不是必需的,因为它是自动发现的.如果我错了,请纠正我的理解.

My understanding of JDBC philosophy: (modern) JAR driver uses technique with file META-INF/services/java.sql.Driver to be known for DriverManager. So in correct situation 4th argument (driver class name) is not required because is discovered automatically. Please correct my understanding if I'm wrong.

我在这个意义上使用了活动"一词(非活动"意味着类存在并已加载,但可以用作 jdbc 驱动程序).

I have used word 'active' in this sense ("non active" means class exist and is loaded, but can be used as jdbc driver).

我的最大代码是:

import groovy.sql.Sql
import groovy.sql.DataSet
import java.sql.DriverManager;
import java.util.ServiceLoader;


c =   Class.forName('com.microsoft.sqlserver.jdbc.SQLServerDriver')
DriverManager.getDrivers()
ServiceLoader.load(java.sql.Driver.class)
def db = [url:'jdbc:sqlserver://localhost;...,driver:'com.microsoft.sqlserver.jdbc.SQLServerDriver']
def sql = Sql.newInstance(db )

但仍然没有合适的驱动异常

我用这样的代码枚举事物(在 newInstance() 之前):

I enumerate things with such code (before newInstance() ):

StringBuilder sb = new StringBuilder();
        String grVersion = "brak";

        Enumeration<Driver> dri = DriverManager.getDrivers();

        for (Enumeration<Driver> e = dri; e.hasMoreElements();) {
            Driver e1 = e.nextElement();
            sb.append(e1.getClass().getName()).append(' ');
            sb.append(e1.getMajorVersion()).append('.').append(e1.getMinorVersion());
        }


        // get LOADED drivers niesetty

        ServiceLoader<java.sql.Driver> codecSetLoader = ServiceLoader.load(java.sql.Driver.class);
        for (Driver e1 : codecSetLoader) {
            sb.append(e1.getClass().getName()).append('!');
            sb.append(e1.getMajorVersion()).append('.').append(e1.getMinorVersion());
            sb.append("# ");
        }

并得到

com.mysql.jdbc.Driver 5.1com.mysql.fabric.jdbc.FabricMySQLDriver 5.1com.mysql.jdbc.Driver!5.1# com.mysql.fabric.jdbc.FabricMySQLDriver!5.1# com.microsoft.sqlserver.jdbc.SQLServerDriver!4.0#  
Exception thrown

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=....

    at ConsoleScript11.run(ConsoleScript11:32)

我执行的(基本)代码是 Tomcat 环境仍然可以正常工作.怎么了?

My (basic) code executed is Tomcat environment still working ok. What's the matter?

推荐答案

Autor 的回答:

从菜单中动态"添加驱动程序 JAR 时(如重写)可见但不能作为 JDBC 工作.

When driver JAR is added "dynamic" from menu (like written over) is visible but not working as JDBC.

当驱动程序 JAR 被放入 ...consolelib 目录时(在 Groovy 的其他 JAR'a 之间)一切正常.

When driver JAR is dropped into ...consolelib catalogue (between other JAR'a of Groovy) all OK.

这种程度的调查对我来说已经足够了,也许有人试图找到菜单中的错误.我可以接受(绿色)我自己的答案吗?

This level of investigation is enough for me, maybe someone try to find bug in menu. Can I accept (green) my own answer?

这篇关于Groovy 控制台/jdbc 驱动程序问题的类路径是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆