Now I use gradle as my build tool. One of my tasks needs to access to a mysql database. Following is my gradle script:
import groovy.sql.Sql
buildscript {
dependencies {
classpath files('/usr/share/java/mysql-connector-java.jar')
}
}
task connectToDb << {
def props = [user: 'user', password: 'password', allowMultiQueries: 'true'] as Properties
def url = 'jdbc:mysql://mysqlhost:3306/db'
def driver = 'com.mysql.jdbc.Driver'
def sql = Sql.newInstance(url, props, driver)
sql.eachRow('show tables') { row ->
println row[0]
}
}
I try to run it in a Ubuntu Lucid box but it always fails. The gradle complains with the information:
Execution failed for task ':connectToDb'.
Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
It seems that the build script doesn't include the mysql connector jar library.Can anyone please tell me how to configure the external jar file properly ? Thanks.
This works for me. See this thread about why http://gradle.1045684.n5.nabble.com/using-jdbc-driver-in-a-task-fails-td1435189.html:
import groovy.sql.Sql
repositories {
mavenCentral()
}
configurations {
driver
}
dependencies {
driver group: 'mysql', name: 'mysql-connector-java', version: '5.1.16'
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each {File file ->
loader.addURL(file.toURL())
}
task connectToDb << {
def props = [user: 'user', password: 'password', allowMultiQueries: 'true'] as Properties
def url = 'jdbc:mysql://mysqlhost:3306/db'
def driver = 'com.mysql.jdbc.Driver'
def sql = Sql.newInstance(url, props, driver)
sql.eachRow('show tables') { row ->
println row[0]
}
}
I've had luck with something like this:
buildscript {
dependencies {
classpath fileTree(dir: '/usr/share/java',
includes: ['mysql-connector-java.jar'])
}
}
Frankly, though, in your situation I'd prefer this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'mysql:mysql-connector-java:5.1.16'
}
}
The following snippet works well for connecting and querying database in Microsoft SQL SERVER. Please note that Microsoft does not make jar file 'sqljdbc42.jar'(which is jdbc driver) available on any online repo like MavenCentral(). Therefore, you would need to download this jar from Microsoft's website and save in project workspace directory.
Download 'sqljdbc_4.2.6420.100_enu.exe'
Unzip the downloaded file and go to Microsoft JDBC Driver 4.2 for SQL Server-->sqljdbc_4.2-->enu. Here you will see the file sqljdbc42.jar. Copy the file into project workspace. I copied into dir name 'lib'.
repositories {
flatDir name: 'localRepository', dirs: 'lib'
}
configurations {
driver
}
dependencies {
driver group: 'sql', name: 'sqljdbc42', version:''
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each { File file ->
loader.addURL(file.toURL())
}
task connectToCoreDb << {
def props = [user: 'sa', password: 'shock', allowMultiQueries: 'true'] as Properties
def connectionUrl = "jdbc:sqlserver://cpt-op-01-db1:1433;databaseName=buds"
def driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
def sql = Sql.newInstance(connectionUrl, props, driver)
sql.eachRow('SELECT name, alias, expiry_date FROM [buds].[dbo].[obj_keystore] ) { row ->
println "$row.name $row.alias $row.expiry_date"
}
logger.info "Closing connection..."
sql.close()
}