-
-
Save beradrian/bc9b48fe048100671daa53f8aacff338 to your computer and use it in GitHub Desktop.
Gradle: Using jdbc in build script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternatively you can use https://github.com/skhome/gradle-database-plugin