How to connect to MySQL from script in Gradle
<p>I want to connect to MySQL through <code>build.gradle</code> I do not want to pick driver from some specified folder like below commented line <code>//loader.addURL(file(JDBC_ARCHIVE_PATH).toURL())</code>. I want it to pick driver from dependencies specified in build.gradle like this
<code>dependencies { compile 'mysql:mysql-connector-java:5.1.37' }</code> How can I do that?</p>

<pre><code>task loadDriver {
    URLClassLoader loader = GroovyObject.class.classLoader
    //loader.addURL(file(JDBC_ARCHIVE_PATH).toURL())
    java.sql.DriverManager.registerDriver(loader.loadClass(analyticsDriverClassName).newInstance())
}

// Connect to the database
// The task expects the following properties:
//  * analyticsDbUrl
//  * analyticsDbUsername
//  * analyticsDbPassword
task callDatabase() {
    println "Connecting to database '$analyticsDbUrl' with user '$analyticsDbUsername' ..."
    def sql = groovy.sql.Sql.newInstance(analyticsDbUrl, analyticsDbUsername, analyticsDbPassword)
    println '... connected'
}
</code></pre>