The exception thrown by Jenkins pipeline.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
I've already installed Database Plugin and MySQL Database Plugin.
But still I'm getting this exception.
The pipeline code :
import groovy.sql.Sql
node('master'){
Class.forName("com.mysql.jdbc.Driver")
def sql = Sql.newInstance("jdbc:mysql://10.10.115.38:3306/test", "root","123456", "com.mysql.jdbc.Driver")
def rows = sql.execute "select count(*) from test;"
echo rows.dump()
}
Exception message:
Running on Jenkins in /var/jenkins_home/workspace/test-jdbc
[Pipeline] {
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
I'm using Jenkins version 2.222.1
The reason for this java.lang.ClassNotFoundException: com.mysql.jdbc.Driver is that the MySQL Connector/J jar is missing.
Note: If you are using higher version of MySQL Connector/J, then it is recommended to use com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver
I updated your script.
import groovy.sql.Sql
node('master'){
Class.forName("com.mysql.cj.jdbc.Driver")
def sql = Sql.newInstance("jdbc:mysql://10.10.115.38:3306/test", "root","123456", "com.mysql.cj.jdbc.Driver")
def rows = sql.execute "select count(*) from test;"
echo rows.dump()
}
Download the jar from here : https://mvnrepository.com/artifact/mysql/mysql-connector-java