What is the classpath for Groovy Console / jdbc driver prblem?

This kind of database code is fine in Java (no "c" value bound), but jdbc cannot be used correctly in Groovy console, exception

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

      

The driver class is known as a script (loaded without null, etc.), but probably not registered with Drivermanager?

code (i am trying to use 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've already checked:

  • fresh start Groovy console doesn't have sqljdbc4.jar in classpath.

Natural exception java.lang.ClassNotFoundException

on line 4 Class.forname()

, or if line 4 is commented out and db options with driver name the exception is on line 7Sql.newInstance(db )

Its logical driver class was not found, etc.

1a. if the db options with three arguments (no driver), I assume its legal (and working in other situations) exceptions change to SQLException: No suitable driver

on line 7Sql.newInstance(db )

It also makes sense that DriverManager doesn't know how to resolve the key jdbc:sqlserver

. The driver is not registered, and DriverManager does not have the magic knowledge that the class implements.

2.when I add jar for console classpath (Script / Add jar (s) to classpath) things have changed somewhat. Greater ClassNotFoundException

and variable c

is nonzero (driver class) but SQLException: No suitable driver

continues.

My understanding of the JDBC philosophy: The (modern) JAR driver uses a technique with a file META-INF/services/java.sql.Driver

to be known for DriverManager

. Therefore, in the correct situation, the 4th argument (driver class name) is not required because it is detected automatically. Please correct my understanding if I am wrong.

I used the word "active" in this sense ("inactive" means that the class exists and is loaded, but can be used as a jdbc driver).

My max code:

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 am listing things with code like this (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 (main) code is executing - Tomcat environment is still working fine. What's the matter?

0


source to share


1 answer


Author's abstract answer:

When adding a JAR driver, "dynamic" from the menu (eg overwritten) is displayed but does not work as JDBC.



When the driver JAR falls into the console \ lib directory (between other Groovy JARs), everything is OK.

This level of research is enough for me, maybe someone is trying to find the error in the menu. Can I accept (green) my own answer?

0


source







All Articles