Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 601
Mikael Svensson Avatar asked Oct 13 '16 07:10

Mikael Svensson


Video Answer


1 Answers

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

like image 183
albciff Avatar answered Sep 29 '22 11:09

albciff