Skip to content

Instantly share code, notes, and snippets.

@beradrian
Forked from xconnecting/build.gradle
Last active May 25, 2017 13:32
Show Gist options
  • Save beradrian/bc9b48fe048100671daa53f8aacff338 to your computer and use it in GitHub Desktop.
Save beradrian/bc9b48fe048100671daa53f8aacff338 to your computer and use it in GitHub Desktop.
Gradle: Using jdbc in build script
import groovy.sql.Sql
// more information at http://docs.groovy-lang.org/latest/html/api/groovy/sql/Sql.html
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'maven'
repositories { mavenCentral() }
configurations { driver }
dependencies { driver 'org.xerial:sqlite-jdbc:3.7.2' }
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each {File file ->
loader.addURL(file.toURL())
}
task jdbcSample << {
def sql = Sql.newInstance( 'jdbc:sqlite:test.db' // JDBC connection string
, '' // username
, '' // password
, 'org.sqlite.JDBC' ) // driver class
sql.execute '''\
CREATE TABLE IF NOT EXISTS person(
id int primary key
,name varchar
)
'''
sql.execute '''\
INSERT OR IGNORE INTO person VALUES(
1
,'test'
)
'''
sql.eachRow( 'select * from person' ) {
int id = it.id
println "id:$id name:$it.name"
}
// load and execute a SQL script file
String sqlString = new File('/path/to/script.sql').text
sql.execute(sqlString);
// close connection
sql.close()
}
@beradrian
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment