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 | |
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' | |
, '' | |
, '' | |
, 'org.sqlite.JDBC' ) | |
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" | |
} | |
sql.close() | |
} |
ビルドスクリプトでJDBCドライバを使用するには、Groovyのクラスローダーにドライバのクラスをロードしておく必要があるらしい。上記のビルドスクリプトを実行した結果は、以下の通り。
$ gradle -q jdbcSample id:1 name:test
0 件のコメント:
コメントを投稿