JDBC driver com.mysql.jdbc.Driver could not be loaded

Hi Guys,

I am fairly new to gradle. Here is my script:

apply plugin: ‘java’

buildscript {

repositories {

mavenCentral()

}

dependencies {

‘mysql:mysql-connector-java:5.1.16’

} }

task buildTravelDatabase << {

println “Connecting to mysql …”

runScript(“src/travel.sql”, “jdbc:mysql://localhost:3306/test”, “root”, “password”) }

def runScript(scriptName, url, user, password) {

ant.sql(src:scriptName,

driver:‘com.mysql.jdbc.Driver’,

print:true,

url:url,

userid:user,

password:password,

onerror:‘continue’

) }

Maven is able to download the mysql driver jar file into gradle cache still I am getting the below error :

Class Not Found: JDBC driver com.mysql.jdbc.Driver could not be loaded

Can you please help me.

Suresh

First, please enclose all code snippets in HTML ‘code’ tags.

Second, your ‘dependencies’ block should look like this:

dependencies {
       classpath 'mysql:mysql-connector-java:5.1.16'
  }

Does this solve the problem?

Peter, Thanks for the response.

I will make sure to embed code in tags.

I am still getting the same exception.

What could be the issue ?

Actually, you probably have to put the Jar on the Ant task’s class path, not on Gradle’s build script class path. Try something like this (without using a ‘buildscript’ block):

configurations {
    sql
}
  dependencies {
    sql 'mysql:mysql-connector-java:5.1.16'
}
  task buildTravelDatabase << {
    ant.sql(classpath: configurations.sql.asPath, ...)
}

It worked. Great help, much appreciated.