Reputation: 1
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)
Upvotes: 0
Views: 3626
Reputation: 28599
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
Upvotes: 1