Is it possible to execute sql scripts via gradle and get responses if the execution was successful or not?
Possibly easiest to use Groovy Sql directly http://groovy.codehaus.org/api/groovy/sql/Sql.html
Thanks for the hint =)
I tried this:
def db = [
url: 'jdbc:sqlserver://TestUrl',
user: 'TestUser',
password: 'TestPW',
driver: 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
]
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
Unfortunately i get an error if i want to create a new instance. (Could not create an instance of type ext_Decorated) I use a ms sql server
Do I miss something important?
Regards
What’s the exception? Try running with “-S” on the command line to get a full stack trace.
You possibly need to
import groovy.sql.*
Or perhaps you need to add the sql server jdbc jar to the
buildscript {
dependencies { ... }
}
Hmmm… it seems there’s classpath issues declaring the jdbc jar in the buildscript dependences.
See this thread for a solution http://stackoverflow.com/questions/6329872/how-to-add-external-jar-files-to-gradle-build-script
This was actually very helpfull and solved my problem. Thank you =)
The Code:
import groovy.sql.Sql
defaultTasks 'connectToDb'
repositories {
flatDir name: 'localRepository', dirs: 'lib'
}
configurations {
driver
}
dependencies {
driver group: 'sql', name: 'sqljdbc4'
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each {File file ->
loader.addURL(file.toURL())
}
task connectToDb << {
def props = [user: 'sa', password: 'pw', allowMultiQueries: 'true'] as Properties
def url = 'jdbc:sqlserver://testserver;databaseName=testDB'
def driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver'
def sql = Sql.newInstance(url, props, driver)
//do stuff
sql.close()
}