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

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

问题描述

这种数据库代码在Java环境中没有问题(没有绑定值'c'),但是在Groovy控制台中无法正确使用jdbc,除了

 > java.sql.SQLException:找不到适合jdbc的驱动程序:sqlserver:// localhost; databaseName = 

驱动程序类是已知的脚本(加载没有null等),但可能未注册DriverManager中?



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

  import groovy.sql.Sql 
导入groovy.sql.DataSet
$ b $ = 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. fresh开始Groovy控制台在classpath上没有sqljdbc4.jar。

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

其逻辑驱动程序类别未找到...等等。如果带有3个参数的db参数(没有驱动程序),我认为它的合法(并在其他情况下工作)异常更改为 SQLException:第7行没有合适的驱动程序 Sql.newInstance(db)



这也是合乎逻辑的,DriverManager不知道如何解析键 jdbc:sqlserver 。驱动程序未注册,DriverManager不知道类实现的是什么。

2。
当我将jar添加到控制台classpath(脚本/将jar添加到classpath中)时,情况有所改变。没有更多 ClassNotFoundException 和变量 c 具有非空值(驱动程序类),但 SQLException:我对JDBC理念的理解:(现代)JAR驱动程序使用文件 META- INF / services / java.sql.Driver 被称为 DriverManager
因此,在正确的情况下,第四个参数(驱动器类名称)不是必需的,因为它是自动发现的。
如果我错了,请纠正我的理解。

在这个意义上,我使用过单词'active'(non active表示存在类并且已加载,但可以用作jdbc驱动程序)。



我的最大代码是:

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

$ bc = 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)

但仍然没有合适的驱动程序例外



编辑2:
我列举了这样的代码(在newInstance()之前):

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

枚举<驱动程序> dri = DriverManager.getDrivers(); (Enumeration< Driver> e = dri; e.hasMoreElements();){
Driver e1 = e.nextElement();


sb.append(e1.getClass()。getName())。append('');
sb.append(e1.getMajorVersion())。append('。')。append(e1.getMinorVersion());
}


//获得LOADED驱动程序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#
抛出异常

java.sql.SQLException:找不到适合jdbc的驱动程序:sqlserver:// localhost; databaseName = ... 。

at ConsoleScript11.run(ConsoleScript11:32)

我的)执行的代码是Tomcat环境仍然正常工作。问题是什么?

解决方案

作者的回答: 当驱动程序JAR从菜单中添加动态(就像写过)是可见的,但不能用作JDBC。



当驱动程序JAR被放入... console \ lib目录在Groovy的其他JAR'a之间)都可以。



这个级别的调查对我来说已经足够了,也许有人试图在菜单中找到bug。我可以接受(绿色)我自己的答案吗?


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=

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

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 )

EDIT: what I had checked already:

  1. fresh started Groovy console hasn't sqljdbc4.jar on classpath.

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

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

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.

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

My maximal code is:

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 )

but still no suitable driver Exception

EDIT2: 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("# ");
        }

and get

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)

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

解决方案

Autor's answer:

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

When driver JAR is dropped into ...console\lib 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 Console / jdbc驱动程序的类路径是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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