'How to add a JDBC driver to a Jenkins pipeline?

I want to create a database within a pipeline script to be used by the deployed app. But first I started testing the connection. I got this problem:

java.sql.SQLException: No suitable driver found for jdbc:mysql://mysql:3306/test_db

I have the database plugin and the MySQL database plugin installed.

How do I get the JDBC driver?

import groovy.sql.Sql
node{

    def sql = Sql.newInstance("jdbc:mysql://mysql:3306/test_db", "user","passwd", "com.mysql.jdbc.Driver")
    def rows = sql.execute "select count(*) from test_table;"
    echo rows.dump()
}

Update after albciff answer:

My versions of:

Jenkins = 2.19.1

Database plugin = 1.5

Mysql database plugin = 1.1

The latest test script.

import groovy.sql.Sql

Class.forName("com.mysql.jdbc.Driver")

Which throws:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 


Solution 1:[1]

From the MySQL DataBase Plugin documentation you can see that jdbc drivers for MySQL are included:

Note that MySQL JDBC driver is under GPLv2 with FOSS exception. This plugin by itself qualifies under the FOSS exception, but if you are redistributing this plugin, please do check the license terms. Drizzle(+MySQL) Database Plugin is available as an alternative to this plugin, and that one is under the BSD license.

More concretely the actual last version (1.1) for this plugin contains connector version 5.1.38:

Version 1.1 (May 21, 2016) mysql-connector version 5.1.38

So probably in order to have the driver available you have to force the driver to be registered.

To do so use Class.forName("com.mysql.jdbc.Driver") before instantiate the connection in your code:

import groovy.sql.Sql
node{
    Class.forName("com.mysql.jdbc.Driver")
    def sql = Sql.newInstance("jdbc:mysql://mysql:3306/test_db", "user","passwd", "com.mysql.jdbc.Driver")
    def rows = sql.execute "select count(*) from test_table;"
    echo rows.dump()
}

UPDATE:

In order to has the JDBC connector classes available in the Jenkins pipeline groovy scripts you need to update the DataBase plugin to last currently version:

Version 1.5 (May 30, 2016) Pipeline Support

Solution 2:[2]

You can simply add the java connector in the java class path.

If jenkins is running java < 9 you probably will find the right place inside something like that: <java_home>/jre/lib/ext

If jenkins is running java >= 9 you probably will find the right place inside something like that: /usr/share/jenkins/jenkins.war

To find your paths you can check:

  • http://your.jenkins.host/systemInfo (or navigate system info path by GUI) and search for java.ext.dirs or java.class.path
  • http://your.jenkins.host/script (running console script such as System.getProperty("java.ext.dirs") or System.getProperty("java.class.path"))

This snippet can help you with the jenkins.war thing when running inside docker:

#adding extra jars to default jenkins java classpath (/usr/share/jenkins/jenkins.war)
RUN sudo mkdir -p /usr/share/jenkins/WEB-INF/lib/
RUN whereis jar #just to find full jar command classpath to use with sudo
COPY ./jar-ext/groovy/mysql-connector-java-8.0.21.jar /usr/share/jenkins/WEB-INF/lib/
RUN cd /usr/share/jenkins && sudo /opt/java/openjdk/bin/jar -uvf jenkins.war ./WEB-INF/lib/mysql-connector-java-8.0.21.jar

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Marco Hackuno Guassone