0
votes

I want to connect to SQL-DB using groovy script on Jenkins to retrieve data as active choice reactive parameter. I have added odbc and mysql drivers in the directory /usr/java/packages/lib

My groovy script reads:

@Grab('com.oracle:ojdbc6:11.2.0.4')
import groovy.sql.Sql;
import java.util.ServiceLoader;
import java.sql.Driver;
ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
import groovy.sql.Sql

def env = "dev"
def url;
def db_name;
db_name= 'test'
def user = 'user'
def password = 'zzzz'
def instance_name = "aaa"

def sql = Sql.newInstance("jdbc:oracle:thin://35.229.73.45:3306/test", "user", "zzzz", "com.mysql.jdbc.Driver")

sql.execute("select * FROM project")    
sql.close()

The error I am getting is

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:560) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at groovy.sql.Sql.loadDriver(Sql.java:716)

1
Welcome to SO! I have edited your post to improve readability. - B--rian
Does printing java classpath shows all jar.s placed in lib/ path you mentioned? - vinWin
I assume you're trying to connect to an Oracle database (since you're using the Oracle thin driver in the URL) but the exception mentions the mysql driver. So I would remove the mysql driver from the classpath and ensure you have ojdbc6.jar in the classpath. - Jean de Lavarene

1 Answers

1
votes

the java.sql.Driver requires jdbc classes to be loaded by system class loader

Not sure it will work in jenkins but you have to try:

@GrabConfig(systemClassLoader=true)
@Grab('com.oracle:ojdbc6:11.2.0.4')

http://docs.groovy-lang.org/latest/html/documentation/grape.html#Grape-JDBCDrivers