如何将JDBC驱动程序添加到Jenkins管道? [英] How to add a JDBC driver to a Jenkins pipeline?

查看:171
本文介绍了如何将JDBC驱动程序添加到Jenkins管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在管道脚本中创建一个数据库,以供已部署的应用程序使用.但是首先我开始测试连接.我遇到了这个问题:

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

我已经安装了数据库插件和MySQL数据库插件.

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

如何获取JDBC驱动程序?

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()
}

albciff回答后更新:

我的版本:

Jenkins = 2.19.1

Database plugin = 1.5

Mysql database plugin = 1.1

最新的测试脚本.

import groovy.sql.Sql

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

哪个抛出:

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

推荐答案

来自

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

请注意,MySQL JDBC驱动程序在GPLv2下(FOSS例外).这 插件本身符合FOSS例外的条件,但是如果您是 重新分发此插件,请检查许可条款. 可以使用Drizzle(+ MySQL)数据库插件来替代 插件,并且该插件已获得BSD许可.

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.

更具体地说,此插件的实际最新版本(1.1)包含连接器版本5.1.38:

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

1.1版(2016年5月21日)mysql-connector 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.

为此,请在实例化代码之前使用Class.forName("com.mysql.jdbc.Driver"):

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()
}

更新:

为了在 Jenkins管道 groovy脚本中提供 JDBC 连接器类,您需要更新

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:

1.5版(2016年5月30日)管道支持

Version 1.5 (May 30, 2016) Pipeline Support

这篇关于如何将JDBC驱动程序添加到Jenkins管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆